Jive reference manual
List of all members | Public Member Functions | Static Public Attributes
jem::util::Barrier Class Reference

Synchronizes a group of threads. More...

#include <jem/util/Barrier.h>

Public Member Functions

 Barrier (int n)
 Constructs a new barrier instance. More...
 
 ~Barrier ()
 Destroys this barrier instance. More...
 
int wait ()
 Suspends the current thread until the last thread has called this function. More...
 
int size () const
 Returns the number of threads that synchronize on this barrier. More...
 

Static Public Attributes

static const int LAST_THREAD
 An integer constant that is returned by wait(). More...
 

Detailed Description

The Barrier class can be used to synchronize a group of threads. It provides a member function, named wait(), that suspends the calling thread. The threads are resumed when the last thread in the group calls the wait() member function.

See also
Thread, Mutex, Monitor.

Constructor & Destructor Documentation

jem::util::Barrier::Barrier ( int  n)
explicit

Constructs a Barrier object that can be used to synchronize n threads.

Parameters
n- the number of threads that will synchronize on this Barrier object.
Precondition
n > 0
Postcondition
this->size() == n
jem::util::Barrier::~Barrier ( )

Destroys this Barrier object. Suspended threads (that have called the member function wait) will not (and thus never) be resumed.

Member Function Documentation

int jem::util::Barrier::wait ( )

If n is the size of this Barrier, then the first (n - 1) threads calling this member function will be suspended. They will be resumed when the n-th thread calls this function. The n-th call of this function also resets this Barrier object so that it can be used to synchronize the next n threads calling this function.

Returns
LAST_THREAD when this member is called by the last thread (the n-th thread). Else, an integer different from LAST_THREAD is returned.
Exceptions
CancelledException- when a suspended thread is cancelled. Note that cancelling a suspended thread will not affect the normal operation of a Barrier object. That is, all suspended threads will be resumed by the last thread calling the wait function, even if one of the suspended threads is cancelled.
int jem::util::Barrier::size ( ) const

Returns the number of threads that synchronize on this Barrier object.

Returns
The number of threads that synchronize on this barrier.

Member Data Documentation

const int jem::util::Barrier::LAST_THREAD
static

An integer constant that is returned by the wait() member function when it is called by the last thread in the group of threads that synchronizes on this Barrier object.