PTypes: multithreading: trigger

Top: Multithreading: trigger

>     #include <pasync.h>

>     class trigger {
>         trigger(bool autoreset, bool initstate);
>         void wait();
>         void post();
>         void signal();      // alias for post()
>         void reset();
>     }

Trigger is a simple synchronization object typically used to notify one or more threads about some event. Trigger can be viewed as a simplified semaphore, which has only two states and does not count the number of wait's and post's. Multiple threads can wait for an event to occur; either one thread or all threads waiting on a trigger can be released as soon as some other thread signals the trigger object. Auto-reset triggers release only one thread each time post() is called, and manual-reset triggers release all waiting threads at once. Trigger mimics the Win32 Event object.

trigger::trigger(bool autoreset, bool initstate) creates a trigger object with the initial state initstate. The autoreset feature defines whether the trigger object will automatically reset its state back to non-signaled when post() is called.

void trigger::wait() waits until the state of the trigger object becomes signaled, or returns immediately if the object is in signaled state already.

void trigger::post() signals the trigger object. If this is an auto-reset trigger, only one thread will be released and the state of the object will be set to non-signaled. If this is a manual-reset trigger, the state of the object is set to signaled and all threads waiting on the object are being released. Subsequent calls to wait() from any number of concurrent threads will return immediately.

void trigger::signal() is an alias for post().

void trigger::reset() resets the state of the trigger object to non-signaled.

See also: thread, mutex, rwlock, semaphore, Examples

PTypes home