Rogue Wave banner
Previous fileTop of DocumentContentsIndex pageNext file
Threads Module User's Guide
Rogue Wave web site:  Home Page  |  Main Documentation Page

5.4 Using Interthread Communication

This section uses a series of simple examples to introduce and explain the concepts and classes involved in interthread communication.

You can find more examples in buildspace\examples\itc. The following list gives the names of those examples and the classes that they exercise:

5.4.1 Using Producer-Consumer Queues

The concept of producer-consumer synchronization was introduced in Section 4.4.5, "Using Condition Variables." This form of synchronization can be used to coordinate communication between two or more threads. By combining producer-consumer synchronization semantics with a simple templatized queue, you can link threads together into a network of cooperating processing elements.

For more information on producer-consumer synchronization, see Section 5.6, "The Producer-Consumer Classes."

5.4.1.1 The RWTPCValQueue Family of Classes

The RWTPCValQueue class is one of a family of producer-consumer classes that includes thread-safe, synchronized, and buffered exchange of data. Each of the classes uses:

The maximum capacity of the buffer can be specified at the time of construction. The default is a buffer whose capacity is limited only by memory.

5.4.1.2 Example

Example 46 simulates a manufacturing operation where an RWTPCValQueue is used to represent a warehouse of limited capacity that accepts widgets from a production function and then sends these widgets to a shipping function. The production of widgets occurs at a fixed rate, while the shipping rate of widgets varies from widget to widget. This difference in read and write rates eventually results in one function being forced to wait on the other.

Example 46: Using a producer-consumer queue in a manufacturing operation

//1

A simple routine for synchronizing message output.

//2

Test to see if the warehouse queue is currently full, so that an alert can be sent before being blocked inside the write function.

//3

Put a widget into the warehouse, and wait if it is full.

//4

Test to see if the warehouse queue is currently empty, so that an alert can be sent before being blocked inside the read function.

//5

Get a widget from the warehouse, and wait if it is empty.

//6

Construct a queue to represent the warehouse and limit the queue length to five entries.



Previous fileTop of DocumentContentsNo linkNext file

Copyright © Rogue Wave Software, Inc. All Rights Reserved.

The Rogue Wave name and logo, and SourcePro, are registered trademarks of Rogue Wave Software. All other trademarks are the property of their respective owners.
Provide feedback to Rogue Wave about its documentation.