The Producer/Consumer pattern is one of the most widely used parallel programming patterns. The most natural approach is to organize your application for processing work items on another thread. In this case, we get two application parts—one puts new work to be processed and the other checks for new work and performs element processing. The standard .NET Framework thread pool is a good example; one thread puts a work item in a processing queue by calling the Task.Run
function of the ThreadPool.QueueUserWorkItem
methods, and the infrastructure finds other threads to process these tasks.
Note
The other parallel programming patterns will be reviewed in the next chapter. The Producer/Consumer pattern is very tightly related to concurrent data structures, and it is more naturally described along with them.
Another classic example is a user interface programming. To create responsive and fast UI, a UI thread has to offload as much work as possible to other threads....