voidfireworks () {
std::unique_lock<std::timed_mutex>lck(mtx,std::defer_lock);
//waiting to get a lock: each thread prints "-" every 200ms:
while(!lck.try_lock_for(std::chrono::milliseconds(200))) {
std::cout<<"-";
}
//got a lock! - wait for 1s, then this thread prints "*"
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
std::cout<<"*\n";
}
voidfireworks () {
std::unique_lock<std::timed_mutex>lck(mtx,std::defer_lock);
//waiting to get a lock: each thread prints "-" every 200ms:
while(!lck.try_lock_for(std::chrono::milliseconds(200))) {
std::cout<<"-";
}
//got a lock! - wait for 1s, then this thread prints "*"
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
std::cout<<"*\n";
}