]> git.lizzy.rs Git - rust.git/commit
Fix `thread` `park`/`unpark` synchronization
authorJames Duley <james.duley@arm.com>
Tue, 11 Sep 2018 17:04:33 +0000 (18:04 +0100)
committerJames Duley <james.duley@arm.com>
Wed, 12 Sep 2018 17:55:44 +0000 (18:55 +0100)
commit204d9608e35f7dc56e179d4539e931766fd88f28
treeec8b05190dd4caa304adbb892b19d623fceeacdc
parent7ee72070bdb789f58f272fab50d49bd48dd9c11f
Fix `thread` `park`/`unpark` synchronization

Previously the code below would not be guaranteed to exit when the first
spawned thread took the `return, // already unparked` path because there
was no write to synchronize with a read in `park`.

```
use std::sync::atomic::{AtomicBool, Ordering};
use std::thread::{current, spawn, park};

static FLAG: AtomicBool = AtomicBool::new(false);

fn main() {
    let thread_0 = current();
    spawn(move || {
        FLAG.store(true, Ordering::Relaxed);
        thread_0.unpark();
    });

    let thread_0 = current();
    spawn(move || {
        thread_0.unpark();
    });

    while !FLAG.load(Ordering::Relaxed) {
        park();
    }
}
```
src/libstd/thread/mod.rs