You.i Engine
CYICountDownLatch Class Reference

Detailed Description

A simple synchronization tool that allows one or more thread to wait until a specific condition is reached.

A CYICountDownLatch instance is created with a Count. The count can be decreased (no lower than 0) by a call to CountDown(). When the count reaches 0, the Finished signal is triggered. Calls to Wait() will block until the Count reaches 0.

All functions in this class, with the exception of the destructor, are thread-safe.

CYICountDownLatch instances cannot be 'reset' once their Count reaches zero.
If a CYICountDownLatch is created with a Count of 0, the Finished signal will never be triggered. Any call to the Wait() functions will return immediately.
When used between multiple threads (and really this is the only reason why someone would use this class), instances of this object cannot be destroyed safely as it's impossible to know if there's still threads waiting on the latch. For that reason, it is recommended that instances of CYICountDownLatch always be stored and used through an std::shared_ptr.

#include <thread/YiCountDownLatch.h>

Public Member Functions

 CYICountDownLatch (uint32_t count)
 ~CYICountDownLatch ()
void CountDown ()
void Wait () const
bool Wait (uint64_t timeoutMs) const
bool IsFinished () const
uint32_t GetCount () const

Public Attributes

CYISignal Finished
 Called when the Count reaches zero. More...

Constructor & Destructor Documentation

◆ CYICountDownLatch()

CYICountDownLatch::CYICountDownLatch ( uint32_t  count)

◆ ~CYICountDownLatch()

CYICountDownLatch::~CYICountDownLatch ( )

Member Function Documentation

◆ CountDown()

void CYICountDownLatch::CountDown ( )

Reduces the Count held in this object by one. Has no effect if the Count is currently zero.

If a call to this function causes the Count to reach zero, the Finished signal is triggered. Any thread currently blocked on a call to Wait() or WaiT(uint32_t) will be awoken.

◆ GetCount()

uint32_t CYICountDownLatch::GetCount ( ) const
Returns the current Count value.

◆ IsFinished()

bool CYICountDownLatch::IsFinished ( ) const
Returns true if the Count has reached zero.

◆ Wait() [1/2]

void CYICountDownLatch::Wait ( ) const

Waits until the Count of this object reaches zero.

If the Count never reaches zero, this function will block forever.

◆ Wait() [2/2]

bool CYICountDownLatch::Wait ( uint64_t  timeoutMs) const

Waits until the Count of this object reaches zero, or until the specified timeout is reached.

In some circumstances, this function may return before the timeout is reached and before the Count reaches zero (e.g. in case of a 'spurious wakeup')
Returns true if the Count has reached zero.

Member Data Documentation

◆ Finished

CYISignal CYICountDownLatch::Finished

Called when the Count reaches zero.

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