#include <iostream>
#include <mutex>
#include <shared_mutex>
int main()
{
std::shared_timed_mutex MySharedTimedMutex;
std::shared_lock<std::shared_timed_mutex>
MySharedLock(MySharedTimedMutex);
if (MySharedLock.owns_lock())
{
std::cout << "R+ ";
}
else
{
std::cout << "R- ";
}
std::shared_lock<std::shared_timed_mutex>
MySharedLock2(MySharedTimedMutex);
if (MySharedLock2.owns_lock())
{
std::cout << "R+ ";
}
else
{
std::cout << "R- ";
}
MySharedLock.unlock();
MySharedLock2.unlock();
std::unique_lock<std::shared_timed_mutex>
MyUniqueLock(MySharedTimedMutex);
if (MyUniqueLock.owns_lock())
{
std::cout << "W+ ";
}
else
{
std::cout << "W- ";
}
return 0;
}
// Output: R+ R+ W+
Reference: http://en.cppreference.com/w/cpp/thread/shared_lock/shared_lock
Tuesday, October 3, 2017
C++14: Shared Lock Type
C++14 added a Shared Lock Type. Here is an example:
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment