Rogue Wave banner
Previous fileTop of DocumentContentsIndexNext file

5.4 Using Interthread Communication

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 examples\thr0200osfam\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

//1A simple routine for synchronizing message output.
//2Test to see if the warehouse queue is currently full, so that an alert can be sent before being blocked inside the write function.
//3Put a widget into the warehouse, and wait if it is full.
//4Test to see if the warehouse queue is currently empty, so that an alert can be sent before being blocked inside the read function.
//5Get a widget from the warehouse, and wait if it is empty.
//6Construct a queue to represent the warehouse and limit the queue length to five entries.

Previous fileTop of DocumentContentsIndexNext file

©Copyright 2000, Rogue Wave Software, Inc.
Contact Rogue Wave about documentation or support issues.