#How to notify a thread

1 messages · Page 1 of 1 (latest)

lost jackal
#

Is there a built in way to notify a thread?

Like this:


// thread 1
const msg = wait_for_message();

zig

// not thread 1
notify_thread(1, my_message);

I see how to implement this myself, but is there something is stdlib?

toxic sigil
#

For my own education, how would you implement this yourself? With a condition variable (which does exist in the stdlib)?

lost jackal
#

one semaphore per thread and a hash table with thread_id -> semaphore and a global mutex for the hash table

toxic sigil
#

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

lost jackal
#

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()

toxic sigil
#

(I'm no expert in these patterns, you may already be aware)

lost jackal
#

yeah a broadcast could also work, and if the wrong thread is awaken it goes back to sleep

chilly laurel
#

or something like that