TEC
A lightweight C++ library enabling safe, efficient execution in multithreaded and concurrent systems.
Loading...
Searching...
No Matches
tec::Signal Class Reference

A thread-safe signal mechanism for inter-thread synchronization. More...

#include <tec_signal.hpp>

Classes

struct  OnExit
 Helper struct to signal termination on exit. More...
 

Public Member Functions

 Signal ()
 Constructs a signal in the unsignaled state.
 
 Signal (const Signal &)=delete
 
 Signal (Signal &&)=delete
 
void set ()
 Sets the signal to the signaled state and notifies all waiting threads.
 
void wait () const
 Waits indefinitely until the signal is set.
 
template<typename Duration >
bool wait_for (Duration dur) const
 Waits for the signal to be set for a specified duration.
 

Detailed Description

A thread-safe signal mechanism for inter-thread synchronization.

This class provides a simple signal implementation using a mutex and condition variable. It allows threads to wait for a signal to be set, either indefinitely or with a timeout. The signal can be set to notify all waiting threads.

Constructor & Destructor Documentation

◆ Signal()

tec::Signal::Signal ( )
inline

Constructs a signal in the unsignaled state.

Initializes the signal with a false flag, indicating it is not signaled.

Member Function Documentation

◆ set()

void tec::Signal::set ( )
inline

Sets the signal to the signaled state and notifies all waiting threads.

Updates the signal flag to true in a thread-safe manner and notifies all threads waiting on the condition variable.

◆ wait()

void tec::Signal::wait ( ) const
inline

Waits indefinitely until the signal is set.

Blocks the calling thread until the signal's flag is set to true. The operation is thread-safe.

◆ wait_for()

template<typename Duration >
bool tec::Signal::wait_for ( Duration  dur) const
inline

Waits for the signal to be set for a specified duration.

Blocks the calling thread until the signal is set or the specified duration elapses. Returns true if the signal was set, false if the timeout occurred.

Template Parameters
DurationThe type of the duration parameter (e.g., std::chrono::milliseconds).
Parameters
durThe maximum duration to wait for the signal.
Returns
bool True if the signal was set, false if the wait timed out.
See also
wait()

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