8 releases (4 breaking)

0.4.3 Nov 14, 2019
0.4.2 Nov 14, 2019
0.4.1 Oct 28, 2019
0.4.0 May 14, 2019
0.0.0 Jun 19, 2018

#2777 in Rust patterns

Download history 10/week @ 2023-06-10 39/week @ 2023-06-17 28/week @ 2023-06-24 71/week @ 2023-07-01 24/week @ 2023-07-08 14/week @ 2023-07-15 19/week @ 2023-07-22 35/week @ 2023-07-29 40/week @ 2023-08-05 32/week @ 2023-08-12 20/week @ 2023-08-19 33/week @ 2023-08-26 35/week @ 2023-09-02 23/week @ 2023-09-09 25/week @ 2023-09-16 21/week @ 2023-09-23

105 downloads per month
Used in breezy-timer

MIT license

17KB
278 lines

Type-level safe mutable global access, with support for recursive immutable locking.

use global::Global;

// The global value.
static VALUE: Global<i32> = Global::new();

// Spawn 100 threads and join them all.
let mut threads = Vec::new();

for _ in 0..100 {
    threads.push(std::thread::spawn(|| {
        *VALUE.lock_mut().unwrap() += 1;
    }));
}

for thread in threads {
    thread.join().unwrap();
}

// This value is guaranteed to be 100.
assert_eq!(*VALUE.lock().unwrap(), 100);

Dependencies

~0.6–3.5MB
~63K SLoC