本文整理汇总了C++中Monitor::notify方法的典型用法代码示例。如果您正苦于以下问题:C++ Monitor::notify方法的具体用法?C++ Monitor::notify怎么用?C++ Monitor::notify使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Monitor
的用法示例。
在下文中一共展示了Monitor::notify方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: run
void run() {
m.wait();
cout << "Done waiting in second thread." << endl;
sleep_in_seconds(5);
m.notify();
sleep_in_seconds(5);
}
示例2: main
int main (int argc, char *argv[]) {
Thread t;
t.start(*new MarkTime);
sleep_in_seconds(5);
m.notify();
m.wait();
cout << "Done waiting in main thread." << endl;
t.lock();
return 0;
// It will take a while for the return to complete, because it
// locks t, which waits on the completion of its thread.
}
示例3: lock
void
MonitorRecMutexTest::run()
{
Monitor<RecMutex> monitor;
MonitorRecMutexTestThreadPtr t;
MonitorRecMutexTestThread2Ptr t2;
MonitorRecMutexTestThread2Ptr t3;
ThreadControl control;
ThreadControl control2;
{
Monitor<RecMutex>::Lock lock(monitor);
Monitor<RecMutex>::TryLock lock2(monitor);
test(lock2.acquired());
// TEST: TryLock
Monitor<RecMutex>::TryLock tlock(monitor);
test(tlock.acquired());
// TEST: Start thread, try to acquire the mutex.
t = new MonitorRecMutexTestThread(monitor);
control = t->start();
// TEST: Wait until the tryLock has been tested.
t->waitTryLock();
}
//
// TEST: Once the mutex has been released, the thread should
// acquire the mutex and then terminate.
//
control.join();
// TEST: notify() wakes one consumer.
t2 = new MonitorRecMutexTestThread2(monitor);
control = t2->start();
t3 = new MonitorRecMutexTestThread2(monitor);
control2 = t3->start();
// Give the thread time to start waiting.
ThreadControl::sleep(Time::seconds(1));
{
Monitor<RecMutex>::Lock lock(monitor);
monitor.notify();
}
// Give one thread time to terminate
ThreadControl::sleep(Time::seconds(1));
test((t2->finished && !t3->finished) || (t3->finished && !t2->finished));
{
Monitor<RecMutex>::Lock lock(monitor);
monitor.notify();
}
control.join();
control2.join();
// TEST: notifyAll() wakes one consumer.
t2 = new MonitorRecMutexTestThread2(monitor);
control = t2->start();
t3 = new MonitorRecMutexTestThread2(monitor);
control2 = t3->start();
// Give the threads time to start waiting.
ThreadControl::sleep(Time::seconds(1));
{
Monitor<RecMutex>::Lock lock(monitor);
monitor.notifyAll();
}
control.join();
control2.join();
// TEST: timedWait
{
Monitor<RecMutex>::Lock lock(monitor);
test(!monitor.timedWait(Time::milliSeconds(500)));
}
}
示例4: notify
bool notify() {
if (_monitor != NULL) {
return _monitor->notify();
}
return true;
}
示例5: lock
void
MonitorMutexTest::run()
{
Monitor<Mutex> monitor;
MonitorMutexTestThreadPtr t;
MonitorMutexTestThread2Ptr t2;
MonitorMutexTestThread2Ptr t3;
ThreadControl control;
ThreadControl control2;
{
Monitor<Mutex>::Lock lock(monitor);
try
{
Monitor<Mutex>::TryLock tlock(monitor);
test(!tlock.acquired());
}
catch(const ThreadLockedException&)
{
//
// pthread_mutex_trylock returns EDEADLK in FreeBSD's new threading implementation
// as well as in Fedora Core 5.
//
}
// TEST: Start thread, try to acquire the mutex.
t = new MonitorMutexTestThread(monitor);
control = t->start();
// TEST: Wait until the tryLock has been tested.
t->waitTryLock();
}
//
// TEST: Once the mutex has been released, the thread should
// acquire the mutex and then terminate.
//
control.join();
// TEST: notify() wakes one consumer.
t2 = new MonitorMutexTestThread2(monitor);
control = t2->start();
t3 = new MonitorMutexTestThread2(monitor);
control2 = t3->start();
// Give the thread time to start waiting.
ThreadControl::sleep(Time::seconds(1));
{
Monitor<Mutex>::Lock lock(monitor);
monitor.notify();
}
// Give one thread time to terminate
ThreadControl::sleep(Time::seconds(1));
test((t2->finished && !t3->finished) || (t3->finished && !t2->finished));
{
Monitor<Mutex>::Lock lock(monitor);
monitor.notify();
}
control.join();
control2.join();
// TEST: notifyAll() wakes one consumer.
t2 = new MonitorMutexTestThread2(monitor);
control = t2->start();
t3 = new MonitorMutexTestThread2(monitor);
control2 = t3->start();
// Give the threads time to start waiting.
ThreadControl::sleep(Time::seconds(1));
{
Monitor<Mutex>::Lock lock(monitor);
monitor.notifyAll();
}
control.join();
control2.join();
// TEST: timedWait
{
Monitor<Mutex>::Lock lock(monitor);
try
{
monitor.timedWait(Time::milliSeconds(-1));
test(false);
}
catch(const IceUtil::InvalidTimeoutException&)
{
}
test(!monitor.timedWait(Time::milliSeconds(500)));
}
}
示例6: run
void MonitorMutxTest::run()
{
Monitor<Mutex> monitor;
MonitorMutexTestThreadPtr t1;
MonitorMutexTestThread2Ptr t2;
MonitorMutexTestThread2Ptr t3;
{
Monitor<Mutex>::Lock lock(monitor);
try
{
Monitor<Mutex>::TryLock tlock(monitor);
test(!tlock.acquired());
}
catch( const ThreadLockedException& e )
{
cout<<"thread locked Execption: "<<e<<endl;
}
t1 = new MonitorMutexTestThread(monitor);
t1->start();
t1->waitTryLock();
}
t1->join();
// test notify()
t2 = new MonitorMutexTestThread2(monitor);
t2->start();
t3 = new MonitorMutexTestThread2(monitor);
t3->start();
Thread::ssleep(Time::seconds(1));
{
Monitor<Mutex>::Lock lock(monitor);
monitor.notify();
}
Thread::ssleep(Time::seconds(1));
test((t2->_finished && !t3->_finished )
|| (t3->_finished && !t2->_finished));
{
Monitor<Mutex>::Lock lock(monitor);
monitor.notify();
}
t2->join();
t3->join();
Thread::ssleep(Time::seconds(1));
//test notifyAll()
t2 = new MonitorMutexTestThread2(monitor);
t2->start();
t3 = new MonitorMutexTestThread2(monitor);
t3->start();
Thread::ssleep(Time::seconds(1));
{
Monitor<Mutex>::Lock lock(monitor);
monitor.notifyAll();
}
Thread::ssleep(Time::seconds(1));
t2->join();
t3->join();
// test timeWait()
{
Monitor<Mutex>::Lock lock(monitor);
try
{
monitor.timedWait(Time::milliSeconds(-1));
test(false);
}
catch( const std::exception& ex )
{
}
const bool bRet = monitor.timedWait(Time::milliSeconds(500));
cout<<"bRet: "<<bRet<<endl;
test(bRet);
}
}