condvar: Using Condition Variables

Examples that explains how to use condition variables.

condvarglobal

This example shows the use of the global condition variable IPC mechanism. The scope of a global condition variable is inter-process. It can be shared by threads of any process in the system.

Download

Click on the following link to download the example: condvarglobal .zip

Click browse to view the example code

Class summary

RCondVar RMutex RChunk CPeriodic

Description

This example uses an adder and subtractor pattern to show the use of a global condition variable. Two processes, the adder and the subtractor, modify a shared variable by adding and subtracting random amounts. The condition variable ensures that the value remains within given limits, KMaxValue and KMinValue , by blocking one of the processes if the value crosses a warning threshold.

The adder program creates a global shared memory chunk. It also creates a global mutex to control access to the chunk and a global condition variable to signal that the value in the chunk is ' ready for use '. It then initialises the value in the chunk to zero and periodically tries to add a random value between 1 and 10 . If, having added a value, it finds that the value of the chunk is greater than KUpperThreshold , it waits for a signal from the condition variable before adding another value.

The subtractor program periodically tries to subtract a random value between 1 and 10 from the global shared memory chunk. If, having subtracted a value, it finds that the value of the chunk is less than KLowerThreshold , it waits for a signal from the condition variable before subtracting another value.

Design and implementation

Build

The ConditionVariable example builds the following binary files in the standard location ( \epoc32\release\winscw\ <build_variant> ) for Carbide.c++.

  • adder.exe: Demonstrates the use of the global condition variable. It creates a global shared memory chunk and periodically adds random values to the chunk.

  • subtractor.exe: Demonstrates the use of the global condition variable. It periodically subtracts random values from the chunk created by adder.exe .

How to run the Example

To run the example, perform the following steps:

  1. Run adder.exe .

  2. Run eshell.

  3. Run subtract.exe in your new eshell.

  4. Switch between eshells by pressing CTRL+ALT+SHIFT+T .

  5. To finish, stop each application by pressing any key.

condvarlocal

This example shows the use of the local condition variable IPC mechanism. The scope of a local condition variable is intra-process. It can be shared by threads of the process that creates the condition variable.

Download

Click on the following link to download the example: condvarlocal .zip

Click browse to view the example.

Class summary

RMutex RThread CPeriodic RCondVar

Description

This example uses the producer and the consumer model to show the use of the local condition variable.

The example creates two local threads: a producer and a consumer. The two threads share a buffer, which is an object of the CQueue class. The CQueue object creates a local condition variable using the RCondVar::CreateLocal() function. It also defines the methods to insert and remove a token from the queue. The CQueue::Insert() function inserts a token into the queue and signals the condition variable. The CQueue::Remove() function tries to remove a token from the queue. If the queue is empty, it must wait for a signal from the condition variable.

An object of the CProducer class creates and calls the producer thread. The producer thread is called once every two seconds using an object of the CPeriodic class. This thread inserts a token into the queue when it is called. It calls the CQueue::Insert() function on the shared CQueue object.

An object of the CConsumer class creates and calls the consumer thread. The consumer thread is called once a second using an object of the CPeriodic class. This thread removes a token from the queue when it is called. It calls the CQueue::Remove() function on the shared CQueue object.

For more information, refer to Condition Variables .

Note: Symbian is not responsible for the content of external websites.

Design and implementation

Build

The Symbian build process describes how to build this example. The ConditionVariable example builds the following binary files in the standard location ( \epoc32\release\winscw\ <build_variant> ) for Carbide.c++.

condvarlocal.exe : Demonstrates the use of the local condition variable.

How to run the Example

To run the example, perform the following steps:

  1. Run condvarlocal.exe . The program calls the producer and the consumer threads periodically as shown in the description section. It also displays a menu.

  2. Press ā€˜dā€™ to display the contents of the queue.

  3. Press ā€˜pā€™ to insert a token into the queue.

  4. Press any key to stop the program.