diff -r 80ef3a206772 -r 48780e181b38 Symbian3/SDK/Source/GUID-5C1E776D-5AEF-5326-BEA6-F2108F42CB71.dita --- a/Symbian3/SDK/Source/GUID-5C1E776D-5AEF-5326-BEA6-F2108F42CB71.dita Fri Jul 16 17:23:46 2010 +0100 +++ b/Symbian3/SDK/Source/GUID-5C1E776D-5AEF-5326-BEA6-F2108F42CB71.dita Tue Jul 20 12:00:49 2010 +0100 @@ -9,96 +9,84 @@ --> -condvar: -Using Condition VariablesExamples that explains how to use condition variables. +condvar: Using Condition VariablesExamples that explains how to use condition variables.

  • condvarglobal

  • condvarlocal

-
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 -Symbian build process describes how to build this example. The ConditionVariable -example builds the following binary files in the standard location (\epoc32\release\winscw\ +

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. 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.

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

  4. +

    How to run the Example

    To run the example, +perform the following steps:

    1. Run adder.exe.

    2. Run eshell.

    3. -
    4. Run subtract.exe in -your new eshell.

    5. -
    6. Switch between eshells -by pressing CTRL+ALT+SHIFT+T.

    7. -
    8. To finish, stop each -application by pressing any key.

    9. +
    10. Run subtract.exe in your new eshell.

    11. +
    12. Switch between +eshells by pressing CTRL+ALT+SHIFT+T.

    13. +
    14. 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.

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. -
  3. Press ‘d’ to -display the contents of the queue.

  4. -
  5. Press ‘p’ to -insert a token into the queue.

  6. -
  7. Press any key to stop -the program.

  8. +
    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.

    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. +
    3. Press ‘d’ to display the contents of the queue.

    4. +
    5. Press ‘p’ to insert a token into the queue.

    6. +
    7. Press any key +to stop the program.

    \ No newline at end of file