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; }