void run() {
        m.wait();
	cout << "Done waiting in second thread." << endl;
	sleep_in_seconds(5);
	m.notify();
	sleep_in_seconds(5);
    }
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.
}
Ejemplo n.º 3
0
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)));
    }
}
Ejemplo n.º 4
0
 bool notify() {
   if (_monitor != NULL) {
     return _monitor->notify();
   }
   return true;
 }
Ejemplo n.º 5
0
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)));
    }
}
Ejemplo n.º 6
0
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);
    }
}