PIP 5.5.3
Platform-Independent Primitives
Public Member Functions | List of all members
PIThreadNotifier Class Reference

Class for simple notify and wait in different threads. More...

#include <pithreadnotifier.h>

Public Member Functions

void wait ()
 Start waiting, return if other thread call notify() More...
 
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. More...
 

Detailed Description

Class for simple notify and wait in different threads.

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

class Worker: public PIThread {
PIOBJECT_SUBCLASS(Worker, 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[]) {
// 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
String class.
Definition: pistring.h:42
Thread class.
Definition: pithread.h:72
Class for simple notify and wait in different threads.
Definition: pithreadnotifier.h:32
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:457
double elapsed_m() const
Returns milliseconds elapsed from last reset() execution or from timer measurer creation.
Definition: pisystemtime.cpp:297
Sequence linear container - dynamic size array of any type.
Definition: pivector.h:118
void piMSleep(double msecs)
Precise sleep for "msecs" milliseconds.
Definition: pitime.h:42

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.