KanoopCommonQt 2.1.1
Kanoop foundational Qt utility library
Loading...
Searching...
No Matches
MutexEvent Class Reference

MutexEvent. More...

#include <mutexevent.h>

Public Member Functions

 MutexEvent (bool wakeAllWaiters=false)
 Construct a MutexEvent.
 
virtual ~MutexEvent ()
 Virtual destructor.
 
void set ()
 Signal the event, waking one (or all) waiting thread(s).
 
void clear ()
 Reset the event to the unsignalled state.
 
bool wait (int msecs=0)
 Block the calling thread until the event is set or the timeout elapses.
 
bool wait (const TimeSpan &timeout)
 Block the calling thread until the event is set or the timeout elapses.
 
bool isWaiting () const
 Test whether a thread is currently blocked in wait().
 
QVariant data () const
 Return the arbitrary data payload attached to this event.
 
void setData (const QVariant &value)
 Attach arbitrary data to this event for use by the woken thread.
 
void setDebug ()
 Enable verbose debug output for this event.
 
void setWakeAllWaiters (bool value)
 Control whether set() wakes all waiting threads or just one.
 

Detailed Description

MutexEvent.

A very useful object for multi-threaded applications which wish to be be event driven.

It is based on the C# AutoResetEvent class which combines a mutex and a cond_var.

The event driven thread call wait() with an optional timeout.

The event generating thread calls set() and the waiting thread wakes up.

Stephen Punak, May 15 2020

A mutex/condition-variable pair modelling the C# AutoResetEvent.

One thread calls wait() and blocks until another thread calls set(). The event auto-resets after each wakeup unless wakeAllWaiters is enabled, in which case all blocked waiters are woken simultaneously.

Definition at line 31 of file mutexevent.h.

Constructor & Destructor Documentation

◆ MutexEvent()

MutexEvent::MutexEvent ( bool  wakeAllWaiters = false)
inline

Construct a MutexEvent.

Parameters
wakeAllWaitersIf true, set() will wake all waiting threads instead of just one

Definition at line 38 of file mutexevent.h.

◆ ~MutexEvent()

virtual MutexEvent::~MutexEvent ( )
inlinevirtual

Virtual destructor.

Definition at line 45 of file mutexevent.h.

Member Function Documentation

◆ data()

QVariant MutexEvent::data ( ) const
inline

Return the arbitrary data payload attached to this event.

Returns
Stored QVariant data

Definition at line 80 of file mutexevent.h.

◆ isWaiting()

bool MutexEvent::isWaiting ( ) const
inline

Test whether a thread is currently blocked in wait().

Returns
true if a thread is waiting

Definition at line 74 of file mutexevent.h.

◆ setData()

void MutexEvent::setData ( const QVariant &  value)
inline

Attach arbitrary data to this event for use by the woken thread.

Parameters
valueData to store

Definition at line 86 of file mutexevent.h.

◆ setDebug()

void MutexEvent::setDebug ( )
inline

Enable verbose debug output for this event.

Definition at line 89 of file mutexevent.h.

◆ setWakeAllWaiters()

void MutexEvent::setWakeAllWaiters ( bool  value)
inline

Control whether set() wakes all waiting threads or just one.

Parameters
valueIf true, all waiters are woken; if false, only one is woken

Definition at line 95 of file mutexevent.h.

◆ wait() [1/2]

bool MutexEvent::wait ( const TimeSpan timeout)
inline

Block the calling thread until the event is set or the timeout elapses.

Parameters
timeoutMaximum wait duration
Returns
true if the event was set, false if the timeout elapsed

Definition at line 65 of file mutexevent.h.

References TimeSpan::totalMilliseconds().

◆ wait() [2/2]

bool MutexEvent::wait ( int  msecs = 0)

Block the calling thread until the event is set or the timeout elapses.

Parameters
msecsMaximum wait time in milliseconds; 0 means wait indefinitely
Returns
true if the event was set, false if the timeout elapsed

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