I may be a little thick but why not just use a dispatch queue? One queue that holds the task to be done. Each worker when not busy gets work from the dispatch queue. All the worker threads are busy so long as the dispatch queue has work to do and the length of time for any task doesn’t affect this efficiency. This technique works even if communication between the dispatcher (keeper of the work queue) and the workers are on different computers. It also makes the order with which each task is executed fair in that no task gets to execute before others that came before them and no need for locks or queues for each worker thread. Given the above solution, I can’t see any reason for “task stealing”.
@jonaskoelker3 жыл бұрын
My first guess is the coordination overhead. If you have a single global queue instead of one per worker thread, the threads need to coordinate such that they don't duplicate or drop work, which takes time. If each thread has a queue of its own, it doesn't need to coordinate with anything when switching from one task to the next, provided its queue is non-empty. A global queue can enforce a particular ordering on the work being done, but sometimes you don't need that; if you're in that situation, why pay for something you don't need?