#How to notify a thread
1 messages · Page 1 of 1 (latest)
For my own education, how would you implement this yourself? With a condition variable (which does exist in the stdlib)?
one semaphore per thread and a hash table with thread_id -> semaphore and a global mutex for the hash table
If you have only one thread to notify, or if you have multiple and you want to unblock them all (or it doesn't matter which one you unblock first), a std.Thread.Condition would be ideal
on the receiving end you do Semaphore.wait and on the sending end you do global_mutex.lock() sem = hash_map.get(thread_id) sem.post()
(I'm no expert in these patterns, you may already be aware)
yeah a broadcast could also work, and if the wrong thread is awaken it goes back to sleep
no way on std, unless you save conditions in a shared place
or something like that