本文整理汇总了C++中Monitor::broadcast方法的典型用法代码示例。如果您正苦于以下问题:C++ Monitor::broadcast方法的具体用法?C++ Monitor::broadcast怎么用?C++ Monitor::broadcast使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Monitor
的用法示例。
在下文中一共展示了Monitor::broadcast方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: testTimedMonitors
bool testTimedMonitors() {
struct TestThread : public Thread {
int _ms;
TestThread(int ms) : _ms(ms) {}
void run() {
testTimedMonitor.wait(_ms);
passedTimedMonitor++;
}
};
TestThread thread1(15);
TestThread thread2(0); // waits forever
TestThread thread3(0); // waits forever
TestThread thread4(0); // waits forever
TestThread thread5(0); // waits forever
for (int i = 0; i < 50; i++) {
// test waiting for a given time
passedTimedMonitor = 0;
thread1.start(); // wait for 15ms at mutex before resuming
Thread::sleepMs(5);
assert(passedTimedMonitor == 0); // thread1 should not have passed
Thread::sleepMs(15);
assert(passedTimedMonitor == 1); // thread1 should have passed
assert(!thread1.isStarted());
// test signalling a set of threads
passedTimedMonitor = 0;
thread2.start();
thread3.start();
thread4.start();
thread5.start();
Thread::sleepMs(5);
testTimedMonitor.signal(2); // signal 2 threads
Thread::sleepMs(5);
assert(passedTimedMonitor == 2);
testTimedMonitor.signal(1); // signal another thread
Thread::sleepMs(5);
assert(passedTimedMonitor == 3);
testTimedMonitor.broadcast(); // signal last thread
Thread::sleepMs(5);
assert(passedTimedMonitor == 4);
assert(!thread2.isStarted() && !thread3.isStarted() && !thread4.isStarted() && !thread5.isStarted());
// test timed and unlimited waiting
passedTimedMonitor = 0;
thread1.start();
thread2.start(); // with another thread
thread3.start(); // with another thread
Thread::sleepMs(5);
testTimedMonitor.signal(); // explicit signal
Thread::sleepMs(5);
assert(passedTimedMonitor == 1);
// wo do not know which thread passed
assert(!thread1.isStarted() || !thread2.isStarted() || !thread3.isStarted());
if (thread1.isStarted()) {
// thread1 is still running, just wait
Thread::sleepMs(10);
assert(passedTimedMonitor == 2);
}
testTimedMonitor.broadcast(); // explicit signal
Thread::sleepMs(5);
assert(passedTimedMonitor == 3);
assert(!thread1.isStarted() && !thread2.isStarted() && !thread3.isStarted());
// test signalling prior to waiting
passedTimedMonitor = 0;
testTimedMonitor.signal();
thread1.start();
Thread::sleepMs(5);
thread2.start();
Thread::sleepMs(5);
assert(passedTimedMonitor == 1);
assert(!thread1.isStarted());
assert(thread2.isStarted());
testTimedMonitor.signal();
Thread::sleepMs(5);
assert(passedTimedMonitor == 2);
assert(!thread2.isStarted());
}
return true;
}