Condition variables are a very elegant way to do some synchronisation. A thread can wait for a condition becoming
true to continue and a thread can also make a condition come
true. Since a thread is an active part, it can wait for or make a condition which are others waiting for.
Let's have a look at an example (see figure 1 below). Thread X creates an item and stores it. Thread Y is waiting for the item to process it. Now let's say: "thread X is making a condition for thread Y to get to work."
We will have a look at an implementation of this example below.
This is the entire source code of the example. For a walkthrough, please read below.
The following program is a very simple producer-consumer demo. It shows two threads, one that produces some numbers, the second that consumes them. The demo is simple because we only use one place as buffer between those threads. So every time the producer has an item, it has to wait for the consumer to take it. To tell the consumer (sending a signal), that an item is ready we use the condition variable.
As soon as the ten items are produced and consumed, the threads will stop and the demo will end.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
The first part is plain simple header inclusion.
Next we have to define some global variables. Global variables are not my favorite, but they will do for this example program. My usually advise: Do not use global data! Ok, the data we need is a mutex and a condition variable.
We also need some variables which take data for the simple producer-consumer demo.
Next we'll have a look at the consumer:
10 11 12 13 14 15 16 17 18
Nothing special so far. The thread locks the mutex to enter the critical section, prints out which item has been produced, stores it in the pool and decreases the total number of item to produce in the future. The last step is to unlock the mutex; to exit the critical section.
The new thing we're looking at in this tutorial is the condition variable and their handling. So far we didn't really used the condition variable at all, but now we have to send a signal that the pool is full:
What now follows is the usual thread termination statement.
20 21 22
Let's have a look at the consumer.
24 25 26 27 28
So far, there's nothing special, locking the mutex to enter the critical section is standard procedure. Now appears the question, what if there aren't any produced items yet? Here comes the answer: the condition variable that we are waiting for.
This statement will unlock the mutex until the condition gets true, say a signal has been sent. Then the mutex gets locked again (using
pthread_mutex_lock, so no timeout), and the thread continues into the critical section.
30 31 32 33 34 35
The rest of the function is quite normal: printing the data from the pool, resetting the pool and unlocking the mutex, exiting the critical section.
Last but not least, standard procedure to terminate the thread.
The function main is quite normal too. The only difference are the initialisation and the destruction of the condition variable.
Please note: the consumer thread is started first, so it can start to consume as soon as there are items ready. This is a measure of quality, not to run the producer first.
37 38 39 40 41 42 43 44 45 46 47 48 49
Multithreading is fun, isn't it? Go ahead try to implement a producer-consumer system:
a: using a circular buffer (queue) with more than one space
b: using more than one producer
c: using more than one consumer
d: using a queue larger than 1, using multiple producers and consumers
The solutions provided here are not the only one possible implementations. The are many solutions. This ones should give you a hint or if you like to compare your solution with this ones.
Walkthroughs are not provided by this site. Try to read them on your own.
All source code files provided by this page is free to copy, modify, redistribute and use for any purpose. Use them on your own risk. The tutorial is copyrighted by Mario Konrad.