#Trying to create global variable that store an array of HWND winapi with Mutex

15 messages · Page 1 of 1 (latest)

distant rover
#

Hi guys, i trying to create global variable that store an array of HWND winapi with Mutex but the this show the error about *mut HWND__ cannot be sent between threads safely. Someone had do this one please help me. Thank you very much

use parking_lot::Mutex;
use winapi::shared::windef::HWND;
use lazy_static;
lazy_static::lazy_static! {
    pub static ref GLOBAL_HWND_TOOLTIPS: Mutex<[Option<HWND>; define::MAX_TOOLTIPS]> = Mutex::new([Some(std::ptr::null_mut()); define::MAX_TOOLTIPS]);
}
pseudo pasture
#

*mut _ is not Send, so a Mutex<*mut _> is not Sync

#

if you need to have a static Mutex that holds raw mut pointers, my first recommendation would be to find a way to not do that
if you really are sure, then you'll need to newtype struct around the pointer with an unsafe impl Send, and also make sure you aren't doing bad things with that

#

(and as an aside Lazy cells are generally preferable over lazy_static!{})

distant rover
#

so do u have another solution to create global variable that store an array of HWND winapi

pseudo pasture
#

If you're sure you need that, define a new struct that wraps those raw pointers and implement Send for it yourself

distant rover
#
pub struct HWNDWrapper(HWND);
unsafe impl Send for HWNDWrapper {}
unsafe impl Sync for HWNDWrapper {}

like this huh

pseudo pasture
#

something like that yeah

#

maybe with an unsafe fn() to access the inner pointer

distant rover
#

do u think HWNDWrapper(HWND) is correct

pseudo pasture
#

if HWND is the !Send type that you need to proxy

distant rover
pseudo pasture
#

that is the syntax for a newtype struct

distant rover
#

hmm a bit inconvenient for you, can u code example that thing with Lazy cells

#

i'm just a newbie so it's hard for me to know which is correct