FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
FEAT::ThreadFence Class Reference

Thread fence synchronization utility class. More...

#include <thread.hpp>

Public Member Functions

 ThreadFence ()
 constructor More...
 
 ThreadFence (const ThreadFence &)=delete
 delete copy-constructor
 
void close ()
 Close the fence and reset internal state. More...
 
void open (bool okay=true)
 Open the fence and notify all waiting threads. More...
 
ThreadFenceoperator= (const ThreadFence &)=delete
 delete copy-assignment operator
 
bool wait ()
 Wait for the fence to be opened. More...
 

Private Attributes

std::condition_variable _cvar
 the internal condition variable More...
 
std::mutex _mtx
 the internal mutex More...
 
bool _okay
 additional state variable More...
 
bool _open
 current state of the fence: open or closed More...
 

Detailed Description

Thread fence synchronization utility class.

This class implements a "fence" for manual thread synchronization, which is characterized by the following properties:

  • The fence can be either open or closed.
  • If the fence is closed, any thread calling the 'wait' function will block until some other thread opens the fence.
  • If the fence is open, any thread calling the 'wait' function will return immediately without blocking.
  • Any thread can open the fence by calling the 'open' function, which also notifies and unblocks all waiting threads.

In addition to the above properties, this class also stores an additional internal boolean state variable, which is specified by the open() function and is returned by the wait() function.

Author
Peter Zajac

Definition at line 35 of file thread.hpp.

Constructor & Destructor Documentation

◆ ThreadFence()

FEAT::ThreadFence::ThreadFence ( )
inlineexplicit

constructor

Definition at line 49 of file thread.hpp.

Member Function Documentation

◆ close()

void FEAT::ThreadFence::close ( )
inline

Close the fence and reset internal state.

Definition at line 97 of file thread.hpp.

References _mtx, _okay, and _open.

◆ open()

void FEAT::ThreadFence::open ( bool  okay = true)
inline

Open the fence and notify all waiting threads.

This function opens the fence, set the internal state and notifies all waiting threads.

Parameters
[in]okayThe state that is to be returned by the wait() function.

Definition at line 86 of file thread.hpp.

References _cvar, _mtx, _okay, and _open.

◆ wait()

bool FEAT::ThreadFence::wait ( )
inline

Wait for the fence to be opened.

This function causes the calling thread to wait for the fence to be opened by another thread. If the fence is already open at the time when this function is called, then this function returns immediately without blocking the calling thread.

Returns
The internal state as set by the open() function.

Definition at line 68 of file thread.hpp.

References _cvar, _mtx, _okay, and _open.

Member Data Documentation

◆ _cvar

std::condition_variable FEAT::ThreadFence::_cvar
private

the internal condition variable

Definition at line 41 of file thread.hpp.

Referenced by open(), and wait().

◆ _mtx

std::mutex FEAT::ThreadFence::_mtx
private

the internal mutex

Definition at line 39 of file thread.hpp.

Referenced by close(), open(), and wait().

◆ _okay

bool FEAT::ThreadFence::_okay
private

additional state variable

Definition at line 45 of file thread.hpp.

Referenced by close(), open(), and wait().

◆ _open

bool FEAT::ThreadFence::_open
private

current state of the fence: open or closed

Definition at line 43 of file thread.hpp.

Referenced by close(), open(), and wait().


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