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//===----------------------------------------------------------------------===//
8
9// UNSUPPORTED: no-threads
10// UNSUPPORTED: c++03
11
12// Test the interoperation of std::lock_guard with std::mutex, since that is such
13// a common use case.
14
15#include <cassert>
16#include <mutex>
17#include <type_traits>
18#include <functional>
19
20#include "make_test_thread.h"
21#include "test_macros.h"
22
23void do_try_lock(std::mutex& m) { assert(m.try_lock() == false); }
24
25int main(int, char**) {
26 {
27 std::mutex m;
28 {
29 std::lock_guard<std::mutex> lg(m);
30 std::thread t = support::make_test_thread(do_try_lock, std::ref(m));
31 t.join();
32 }
33
34 // This should work because the lock_guard unlocked the mutex when it was destroyed above.
35 m.lock();
36 m.unlock();
37 }
38
39 // Test CTAD
40#if TEST_STD_VER >= 17
41 {
42 std::mutex m;
43 std::lock_guard lg(m);
44 static_assert(std::is_same<decltype(lg), std::lock_guard<std::mutex>>::value, "");
45 }
46#endif
47
48 return 0;
49}
50

Provided by KDAB

Privacy Policy
Learn to use CMake with our Intro Training
Find out more

source code of libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.guard/std.mutex.pass.cpp