O'Reilly logo

.NET Framework 4.5 Expert Programming Cookbook by A.P. Rajshekhar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Handling Producer-Consumer race conditions

The Producer-Consumer problem is one of the classic thread-related problems. Simply put, it can be described as follows:

Two threads, one that writes to the shared resource (Producer) and one that reads from the shared resource (Consumer), should be synchronized so that the Consumer does not consume more than what the Producer has produced.

The approach to handling this problem involves use of semaphores or monitors. We have seen how monitors are used to create a shared resource. We will now use SharedBuffer created in the previous recipe to avoid a race condition in a Producer-Consumer scenario. We will also create a Producer class to write into it and a Consumer class to read from it, and use threads ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required