1 //===----------------------------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // UNSUPPORTED: libcpp-has-no-threads 11 // UNSUPPORTED: c++98, c++03, c++11 12 13 // <shared_mutex> 14 15 // template <class Mutex> class shared_lock; 16 17 // bool try_lock(); 18 19 #include <shared_mutex> 20 #include <cassert> 21 22 bool try_lock_called = false; 23 24 struct mutex 25 { 26 bool try_lock_shared() 27 { 28 try_lock_called = !try_lock_called; 29 return try_lock_called; 30 } 31 void unlock_shared() {} 32 }; 33 34 mutex m; 35 36 int main() 37 { 38 39 std::shared_lock<mutex> lk(m, std::defer_lock); 40 assert(lk.try_lock() == true); 41 assert(try_lock_called == true); 42 assert(lk.owns_lock() == true); 43 try 44 { 45 lk.try_lock(); 46 assert(false); 47 } 48 catch (std::system_error& e) 49 { 50 assert(e.code().value() == EDEADLK); 51 } 52 lk.unlock(); 53 assert(lk.try_lock() == false); 54 assert(try_lock_called == false); 55 assert(lk.owns_lock() == false); 56 lk.release(); 57 try 58 { 59 lk.try_lock(); 60 assert(false); 61 } 62 catch (std::system_error& e) 63 { 64 assert(e.code().value() == EPERM); 65 } 66 } 67