bool wait(duration_t* timeout) { std::unique_lock<std::mutex> guard(mutex); queue_lock->unlock(); std::cv_status res = std::cv_status::no_timeout; if(timeout) { auto start = std::chrono::system_clock::now(); res = ready.wait_for(guard, *timeout); *timeout -= (std::chrono::system_clock::now() - start); } else { ready.wait(guard); } guard.unlock(); queue_lock->lock(); return res == std::cv_status::no_timeout; }
void start ( void) { //---------------------- begin --------------------- assert ( N ==0 ); //std::cout<<"1.-Begin main function \n"; N++ ; s.try_lock() ; N++ ; std::thread T1 ( function1); std::thread T2 ( function2); //std::cout<<"2.-Inside main function \n"; std::this_thread::sleep_for( std::chrono::seconds(3) ) ; assert ( N == 4 ); N++ ; s.unlock() ; T1.join() ; T2.join() ; assert ( N == 9); //std::cout<<"7.-End of main function \n"; }
void after_yield() { lock->unlock(); }