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 12 // <shared_mutex> 13 14 // template <class Mutex> class shared_lock; 15 16 // bool try_lock(); 17 18 #include <shared_mutex> 19 #include <cassert> 20 21 #if _LIBCPP_STD_VER > 11 22 23 bool try_lock_called = false; 24 25 struct mutex 26 { try_lock_sharedmutex27 bool try_lock_shared() 28 { 29 try_lock_called = !try_lock_called; 30 return try_lock_called; 31 } unlock_sharedmutex32 void unlock_shared() {} 33 }; 34 35 mutex m; 36 37 #endif // _LIBCPP_STD_VER > 11 38 main()39int main() 40 { 41 #if _LIBCPP_STD_VER > 11 42 std::shared_lock<mutex> lk(m, std::defer_lock); 43 assert(lk.try_lock() == true); 44 assert(try_lock_called == true); 45 assert(lk.owns_lock() == true); 46 try 47 { 48 lk.try_lock(); 49 assert(false); 50 } 51 catch (std::system_error& e) 52 { 53 assert(e.code().value() == EDEADLK); 54 } 55 lk.unlock(); 56 assert(lk.try_lock() == false); 57 assert(try_lock_called == false); 58 assert(lk.owns_lock() == false); 59 lk.release(); 60 try 61 { 62 lk.try_lock(); 63 assert(false); 64 } 65 catch (std::system_error& e) 66 { 67 assert(e.code().value() == EPERM); 68 } 69 #endif // _LIBCPP_STD_VER > 11 70 } 71