Stream: general

Topic: Joining one thread out of many?

Laurențiu Nicola (Jan 28 2020 at 21:15, on Zulip):

This came up in another thread (sic) -- I don't see any way to wait for one thread out of N to finish (except maybe spawning N more threads that call join on the original ones and send the results over a channel type that supports select).

Amanieu (Jan 28 2020 at 22:49, on Zulip):

This is not possible on Linux at least. On Windows it might be possible with WaitForMultipleObjects, but even then you're limited to 64 objects. Using select and channels is the only portable way.

Laurențiu Nicola (Jan 28 2020 at 22:51, on Zulip):

I see, thanks

Matthias247 (Jan 30 2020 at 06:58, on Zulip):

You can do it in application code: Add a guard in each thread, which signals a shared object (e.g. a condition variable, channel, manualresetevent, etc). Then wait on that object.

If you need to be sure that the thread is really joined and not only close to being joined, then you need to pass each thread some unique ID that you can send back via this channel. Then the waiter can identify the associated JoinHandle and join the thread.

Last update: Jun 07 2020 at 09:35UTC