PIP 5.9.0
Platform-Independent Primitives
Loading...
Searching...
No Matches
PIThreadNotifier Class Reference

Thread notifier class for synchronization between threads. More...

#include <pithreadnotifier.h>

Public Member Functions

void wait ()
 Start waiting, return if other thread call notify().
bool waitFor (PISystemTime timeout)
 Start waiting no longer than "timeout", return true if other thread call notify(), false if timeout expired.
void notify ()
 Notify one waiting thread, which waiting on wait() function.

Detailed Description

Thread notifier class for synchronization between threads.

Class for simple notify and wait in different threads.

Each notify() stores one pending wake-up. A later wait() or waitFor() consumes one stored notification; with multiple waiters the resume order is unspecified.

Synopsis

This class used as event mechanism between threads. One thread wait for some event, and another send this event, unblocking first thread. It is useful to syncronize some actions in several threads.

Usage

PIThreadNotifier notifier;
class Worker: public PIThread {
public:
Worker(const PIString & n) {
setName(n);
}
void run() override {
piCoutObj << (int)time.elapsed_m() << "wait ...";
notifier.wait();
piCoutObj << (int)time.elapsed_m() << "done";
};
};
int main(int argc, char * argv[]) {
PIVector<Worker*> workers;
// create 2 threads
for (auto n: {"first ", "second"})
workers << new Worker(n);
// start them
for (auto * w: workers)
w->startOnce();
piMSleep(500);
notifier.notify(); // notify one of them after 500 ms
piMSleep(500);
notifier.notify(); // notify one of them after 1000 ms
for (auto * w: workers)
w->waitForFinish();
}
// [Worker "first "] 0 wait ...
// [Worker "second"] 0 wait ...
// [Worker "second"] 500 done
// [Worker "first "] 1000 done
#define piCoutObj
Macro used for conditional (piDebug && PIObject::debug()) output to PICout(StdOut) for subclasses of ...
Definition picout.h:45
#define PIOBJECT_SUBCLASS(name, parent)
Put this macro inside a PIObject subclass definition to inherit registered methods and class scope fr...
Definition piobject_macros.h:53
void setName(const PIString &name)
Sets the name property of this object.
Definition piobject.h:178
String class.
Definition pistring.h:42
Thread object that executes work on a dedicated system thread.
Definition pithread.h:99
virtual void run()
Virtual method executed on each loop iteration until stop is requested.
Definition pithread.h:315
void wait()
Start waiting, return if other thread call notify().
Definition pithreadnotifier.cpp:109
void notify()
Notify one waiting thread, which waiting on wait() function.
Definition pithreadnotifier.cpp:139
Time measurements.
Definition pisystemtime.h:456
double elapsed_m() const
Returns milliseconds elapsed from last reset() execution or from timer measurer creation.
Definition pisystemtime.cpp:297
void piMSleep(double msecs)
Precise sleep for "msecs" milliseconds.
Definition pitime.h:41

Member Function Documentation

◆ wait()

void PIThreadNotifier::wait ( )

Start waiting, return if other thread call notify().

If notify() has been called before, then returns immediately.
If notify() has been called "n" times, then returns immediately "n" times, but only if wait in one thread.
If many threads waiting, then if notify() has been called "n" times, all threads total returns "n" times in undefined sequence.

◆ notify()

void PIThreadNotifier::notify ( )

Notify one waiting thread, which waiting on wait() function.

If many threads waiting, then notify randomly one.
If call this "n" times, then notify any waiting threads totally "n" times.