How do you assign work to threads? This episode explains several ways you can think about this and when to use them.

You’ll learn about the following four ways to divide work to be done by your threads:

  • When you have small work items arriving at different times, then assign a task to a thread as needed. You’ll probably want to use a thread pool so that you can avoid the extra overhead of creating and destroying threads for each small task.
  • When you have a bunch of small tasks that are ready to be processed, just divide them up into smaller groups and assign each group to a thread.
  • If it’s not clear how to divide a task or when you have several tasks that could take different amounts of work to complete, then consider a queue where a thread can initially take a piece of work from the queue and either decide to complete the task or divide the work into smaller tasks and put each smaller part back into the queue.
  • And if you have large work items arriving at different times, then consider splitting the work itself into fixed stages and assigning the tasks to a queue for the first stage. Then a thread can take a task out of the first stage queue, perform just its part related to the first stage, and then put the task into another queue for the second stage.