Avneesh
Avneesh

Reputation: 133

boost::thread: notify when first thread in thread_group has finished?

I have a boost::thread_group, which I have initialized using thread_group,

e.g. thread_group tg1;

and populated using create_thread. While using create_thread, the threads start execution.

The various threads in my thread group can finish execution at different times. I know one can use tg1.join_all() if the objective is to wait for all of the threads in the thread group to finish executing.

However, what I would ideally like to do is call this function, wait for the most recently finished thread, remove it from the group and use the thread results in an appropriate manner, and then call the function again (it will either wait if no other thread has finished execution or it will immediately return the next thread that has finished execution, just like a regular join() function) until the group is empty.

Is there any function in the boost libraries that can return the thread ID of the thread that has finished first?

Upvotes: 2

Views: 1081

Answers (1)

John Zwinck
John Zwinck

Reputation: 249153

You could make a queue with a condition variable to wait on. When any thread finishes, it would put its results (or a reference to them or to the thread itself) on this queue, and signal the condition.

Upvotes: 3

Related Questions