Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ion::port::Barrier Class Reference

The Barrier class defines a multi-thread barrier that allows N threads to synchronize execution. More...

#include "barrier.h"

Public Member Functions

 Barrier (uint32 thread_count)
 Constructs an instance that will wait for thread_count threads. More...
 
 ~Barrier ()
 
bool IsValid () const
 Returns true if a valid barrier was created by the constructor. More...
 
void Wait ()
 Causes the current thread to wait at the barrier. More...
 

Detailed Description

The Barrier class defines a multi-thread barrier that allows N threads to synchronize execution.

For example, if you create a Barrier for 3 threads and have each of the three threads call Wait() on it, then execution of each waiting thread will proceed once all 3 have called Wait().

Definition at line 38 of file barrier.h.

Constructor & Destructor Documentation

ion::port::Barrier::Barrier ( uint32  thread_count)
explicit

Constructs an instance that will wait for thread_count threads.

Linux and QNX are the only platforms that support barriers in pthreads.

If thread_count is not positive, the Barrier will do nothing and IsValid() will return false.

Both the windows and non-Linux/QNX implementations below may seem rather complex. This is because they guard against two potential errors:

  • Deadlock between a wait and a broadcast/set event, ensuring that all threads have entered the wait branch before the broadcast.
  • An issue where the Barrier destructor is called before all threads are done using the synchronization objects, causing an intermittent crash. For example, a mutex may be destroyed while it is still being held by pthread_cond_wait(), or a handle can be reset after it has been closed in the destructor. Non-barrier pthreads version.

Pthread barriers are an optional part of the Posix spec, and Mac, iOS, and Android do not support them, so this version is used on those platforms.

Definition at line 156 of file barrier.cc.

References IsValid().

ion::port::Barrier::~Barrier ( )

Definition at line 168 of file barrier.cc.

References IsValid().

Member Function Documentation

bool ion::port::Barrier::IsValid ( ) const
inline

Returns true if a valid barrier was created by the constructor.

If this returns false, the Wait() function is a no-op.

Definition at line 48 of file barrier.h.

Referenced by Barrier(), Wait(), and ~Barrier().

void ion::port::Barrier::Wait ( )

Causes the current thread to wait at the barrier.

Definition at line 182 of file barrier.cc.

References IsValid().


The documentation for this class was generated from the following files: