0

I want to reuse a set of worker threads. Each worker thread performs independent work but they must start and stop processing as a coordinated team. I need an efficient means for each worker thread to block until the main thread tells them all to go, and an efficient means for the main thread to block until all worker threads are finished.

Each chunk of work will only require some tens of microseconds so the usual approach of creating a set of threads then joining them all involves far too much overhead.

The pseudocode is like the following:

main thread:

create N threads

forever
    prepare new independent work for each thread
    tell all N threads to run their part
    wait for all N threads to complete their work
    use results

typical worker thread:

forever
    wait to run
    do my work
    indicate to main my work is complete

My question is how best to perform this signaling and synchronization. I am not asking about how to divide up the work or move work to or from each thread; suffice it to say the threads do not interact.

pidloop
  • 1
  • 2
  • First thing to come to mind is an event driven approach. Have each worker thread subscribe to a readySetGo event. (Or the eventHandler can fire off the threads. Have main subscribe to an allThreadsFinished event. – Traveling Man May 29 '18 at 20:08
  • This is too broad/opinion-based. For example, I might think that semaphores can be used here, but you've dismissed that approach as too kludgy. How can we know what you won't find kludgy? Try showing a specific attempted solution and explain what specifically you don't like about it. – Ken Thomases May 29 '18 at 20:36
  • @Ken: fair enough, I've edited the question to just ask for suggestions. – pidloop May 29 '18 at 22:28
  • @Ken: so are you going to suggest something constructive or just throw rocks? – pidloop Jun 10 '18 at 16:59

0 Answers0