Closed Daniel-Xu closed 1 year ago
That is a great question!
The answer is, fortunately: no, this won't hang forever. However, the reason for that is subtle and not actually fully explained in the book. (I might go a bit deeper into that in a next edition of the book.)
The reason this won't hang forever, is that unpark()
forms a happens-before relationship with the park()
call that it wakes up. (See the comment in the implementation here.)
I'll see if we can document that better and make it a proper guarantee.
Thanks so much @m-ou-se , I got it now after reading the source code of park
& unpark
The content that the question is about
In Chapter 5. Building Our Own Channels
the implementation is from Safety Through Runtime Checks
The question
is it possible for the receiver thread hanging forever?
my reasoning
For
is_ready
, the sender thread storestrue
withRelease
ordering and then unpark theReceiver
thread. After woken up, the receiver thread will check theis_ready
again. However, it's possible that the return value is stillfalse
because we are using simpleload
.This will park the receiver thread again and no other events will wake it up, thus hanging forever.