[MLIR][NVVM] Declare InferIntRangeInterface for RangeableRegisterOp (#122263)
[llvm-project.git] / libcxx / test / std / thread / thread.mutex / thread.mutex.requirements / thread.sharedtimedmutex.requirements / thread.sharedtimedmutex.class / try_lock_shared.pass.cpp
blob6430a3233422750ad7e91aac3e0ad2e8c5ce46e3
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: no-threads
10 // UNSUPPORTED: c++03, c++11
12 // <shared_mutex>
14 // class shared_timed_mutex;
16 // bool try_lock_shared();
18 #include <shared_mutex>
19 #include <cassert>
20 #include <thread>
21 #include <vector>
23 #include "make_test_thread.h"
25 int main(int, char**) {
26 // Try to lock-shared a mutex that is not locked yet. This should succeed.
28 std::shared_timed_mutex m;
29 std::vector<std::thread> threads;
30 for (int i = 0; i != 5; ++i) {
31 threads.push_back(support::make_test_thread([&] {
32 bool succeeded = m.try_lock_shared();
33 assert(succeeded);
34 m.unlock_shared();
35 }));
38 for (auto& t : threads)
39 t.join();
42 // Try to lock-shared a mutex that is already exclusively locked. This should fail.
44 std::shared_timed_mutex m;
45 m.lock();
47 std::vector<std::thread> threads;
48 for (int i = 0; i != 5; ++i) {
49 threads.push_back(support::make_test_thread([&] {
50 bool succeeded = m.try_lock_shared();
51 assert(!succeeded);
52 }));
55 for (auto& t : threads)
56 t.join();
58 m.unlock();
61 // Try to lock-shared a mutex that is already lock-shared. This should succeed.
63 std::shared_timed_mutex m;
64 m.lock_shared();
65 std::vector<std::thread> threads;
66 for (int i = 0; i != 5; ++i) {
67 threads.push_back(support::make_test_thread([&] {
68 bool succeeded = m.try_lock_shared();
69 assert(succeeded);
70 m.unlock_shared();
71 }));
73 m.unlock_shared();
75 for (auto& t : threads)
76 t.join();
79 return 0;