I've been trying to figure out if there's a simple way using condition variables and mutexes to design a function with the following properties (cliffs notes first, and then longer version in case it's not enough info):
1) there is a section of code in the function such that two threads cannot enter that section at the same time.
2) after leaving the section of code, the thread should wait for something to happen (signalled by another thread)
3) the waits in step 2 should be awoken in the order they occured.
Basically there are many threads that may want to post requests to read/write from the network. So I have a bounded buffer implementation that holds "work items", which can be of read requests or write requests. writes return immediately, but reads block until the read is finished, which is determined by when the actual network thread processes the work item and signals something. read requests should be able to enter the queue regardless of whether another read request is already pending, but obviously the blocked threads should awake in the same order that items were inserted into the queue.
I thought of making a condition variable for each read work item, and storing it in the work item that goes in the queue, so the network thread can notify that. But I feel like there should be a better way that doesn't require potentially arbitrary numbers of condition variables.
Thanks