SourcePro® API Reference Guide

 
List of all members | Public Member Functions | Private Member Functions
RWBarrier Class Reference

Synchronizes a number of concurrent threads at a specific point in their processing. More...

#include <rw/sync/RWBarrier.h>

Public Member Functions

 RWBarrier (int count=1)
 
 ~RWBarrier ()
 
void setCount (int count)
 
void wait ()
 

Private Member Functions

 RWBarrier (const RWBarrier &second)
 
RWBarrieroperator= (const RWBarrier &second)
 

Detailed Description

RWBarrier synchronizes a number of concurrent threads at a specific point in their processing. Each thread indicates that it has reached the "barrier" by calling the wait() member function. Each thread is blocked by the call to wait() until the number of threads specified in the RWBarrier constructor have all called wait(). When the last thread calls wait(), all threads waiting at the barrier are unblocked, making them eligible for execution.

Initializing an RWBarrier with a zero count is not legal, and causes an assertion to be triggered in debug mode. Using an RWBarrier with a count different than the number of threads able to perform a wait() on that RWBarrier is outside the intended use of the class. There is no diagnostic available to detect this condition.

The count of an RWBarrier may be changed after initialization by using the setCount(int) function. When calling this function, make sure that no threads are blocked in wait(). If threads are blocked in wait(), an assertion is triggered in debug mode; in release mode, an exception is thrown.

When an RWBarrier is destroyed, no threads should be blocked in the wait() function. When the module is built in debug mode, an assertion is raised if there are threads waiting when the RWBarrier is destructed.

Example
#include <rw/sync/RWBarrier.h>
#include <rw/functor/rwBind.h>
#include <rw/thread/RWThreadFunction.h>
void func(RWBarrier* barrier)
{
// do initialization ...
// wait for all threads to complete initialization
barrier->wait();
}
int main()
{
RWBarrier barrier(3);
RWThread t1 = RWThreadFunction::make(rwBind(func, &barrier));
RWThread t2 = RWThreadFunction::make(rwBind(func, &barrier));
t1.start();
t2.start();
barrier.wait(); // Wait for t1 and t2 to complete
// initialization.
t1.join();
t2.join();
return 0;
}

Constructor & Destructor Documentation

RWBarrier::RWBarrier ( int  count = 1)

Constructs a barrier for synchronizing count threads, where count must be greater than 0. A default count of 1 is provided for default construction. This can be changed following construction using the setCount() method.

Note
Constructing a barrier with count not equal to 1 causes a debug assertion in single-threaded debug mode builds.
RWBarrier::~RWBarrier ( )

Destructor. Triggers an assertion failure in debug mode if there are any threads blocked in wait().

RWBarrier::RWBarrier ( const RWBarrier second)
private

Copy construction prohibited.

Member Function Documentation

RWBarrier& RWBarrier::operator= ( const RWBarrier second)
private

Assignment prohibited.

void RWBarrier::setCount ( int  count)

Sets the barrier count to count. The count must be greater than or equal to 1. There must be no threads blocked in wait() when this function is called.

Note
Passing count not equal to 1 causes an assertion in single-threaded debug mode builds.
void RWBarrier::wait ( )

Each thread calls wait() when it reaches its synchronization point. The threads block in wait() until count threads have called wait(). When the last thread enters wait(), all threads are released.

Note
This function is a no-op in single-threaded builds because there are no other threads with which to synchronize.

Copyright © 2023 Rogue Wave Software, Inc., a Perforce company. All Rights Reserved.