m-ou-se / rust-atomics-and-locks

Code examples, data structures, and links from my book, Rust Atomics and Locks.
Other
1.33k stars 120 forks source link

Possible race condition #42

Closed QuarticCat closed 1 year ago

QuarticCat commented 1 year ago

Type of error

Minor technical mistake

Location of the error

https://github.com/m-ou-se/rust-atomics-and-locks/blob/d945e828bd08719a2d7cb6d758be4611bd90ba2b/src/ch5_channels/s6_blocking.rs

Description of the error

Consider this:

  1. At time point 1, the receiver goes to here -- it finds the state is false, but hasn't parked yet.
  2. At time point 2, the sender goes to here -- it sets the state to true, and tries to unpark the receiver.
  3. At time point 3, the receiver continues -- it parks itself. Then boom, no thread will unpark it.
QuarticCat commented 1 year ago

Oh, I just found that the document says "if unpark happens first, park` will return immediately". Sorry for bothering!

QuarticCat commented 1 year ago

Oh, I just found that the document says "if unpark happens first, park` will return immediately". Sorry for bothering!