问题描述:

Is there a way to wake a specific thread in Haskell? There is a function that suspends the current thread. But the waking counterpart doesn't seem to exist.

网友答案:

yield doesn't suspend the current thread - it moves it to the back of the run queue. It's still in the run queue, it just makes sure that other runnable threads (potentially not all runnable threads, if there are multiple execution contexts defined, which makes this a a pretty weak guarantee) have a chance to run before it continues. For the most part, you should ignore yield. The exception is when you understand exactly what it does, and why that matters.

To actually suspend and resume a thread, MVars are the way to go. When a thread waits on an empty MVar, it is removed from the runnable queue. When a value is put into an MVar, a thread waiting on it (I believe in GHC it's always the thread that has been waiting on that MVar longest, but it's not guaranteed) is put back into the runnable queue.

相关阅读:
Top