#include <iostream>
#include <shared_mutex>
int main()
{
std::shared_timed_mutex MySharedTimedMutex;
if (MySharedTimedMutex.try_lock_shared())
{
std::cout << "R+ ";
}
else
{
std::cout << "R- ";
}
if (MySharedTimedMutex.try_lock_shared())
{
std::cout << "R+ ";
}
else
{
std::cout << "R- ";
}
if (MySharedTimedMutex.try_lock())
{
std::cout << "W+ ";
}
else
{
std::cout << "W- ";
}
MySharedTimedMutex.unlock_shared();
MySharedTimedMutex.unlock_shared();
if (MySharedTimedMutex.try_lock())
{
std::cout << "W+ ";
}
else
{
std::cout << "W- ";
}
return 0;
}
// Output: R+ R+ W- W+
Reference: http://en.cppreference.com/w/cpp/thread/shared_timed_mutex
Tuesday, October 3, 2017
C++14: Shared Timed Mutex
C++14 added a shared timed mutex. You can lock something as shared (like a reader lock) or exclusive (like a writer lock). Here is an example:
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment