parking

Build License Cargo Documentation

Thread parking and unparking.

This is a copy of the park()/unpark() mechanism from the standard library.

What is parking

Conceptually, each Parker has a token which is initially not present:

Analogy with channels

Another way of thinking about Parker is as a bounded channel with capacity of 1.

Then, Parker::park() is equivalent to blocking on a receive operation, and Unparker::unpark() is equivalent to a non-blocking send operation.

Examples

```rust use std::thread; use std::time::Duration; use parking::Parker;

let p = Parker::new(); let u = p.unparker();

// Make the token available. u.unpark(); // Wakes up immediately and consumes the token. p.park();

thread::spawn(move || { thread::sleep(Duration::from_millis(500)); u.unpark(); });

// Wakes up when u.unpark() provides the token, but may also wake up // spuriously before that without consuming the token. p.park(); ```

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.