How to use static Mutex?


#1

In the docs page about Mutex there is a sentence: “The mutex can also be statically initialized or created via a new constructor.”. But when I use “static sys_mutex: Mutex = Mutex::new(0 as u32);” I got an error: “statics are not allowed to have destructors”.

How can I use a global Mutex? I need it from 3-4 crates an it must be reachable from there.

P.S.: I use Rust 1.5, which complains about StaticMutex.


#2

If you want a static Mutex that you can use like a regular one, you might want to look into the lazy_static crate which allows you to create a static with a non-constant initializer.

Keep in mind that this allocates some resources that will never be freed, so diagnostic tools might report a memory leak.