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

Figure 9-1 #52

Closed z5014823 closed 4 months ago

z5014823 commented 5 months ago

The content that the question is about

No response

The question

Isn't the final wake_one() call of the second thread redundant?

m-ou-se commented 5 months ago

If there are no other waiting threads, it is unnecessary. Correct.

However, the unlock() method cannot know at that point if there are other threads waiting or not. There might be other threads waiting (because the state is 2), so it will call wake_one().