1bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant//===----------------------------------------------------------------------===//
2bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant//
3f5256e16dfc425c1d466f6308d4026d529ce9e0bHoward Hinnant//                     The LLVM Compiler Infrastructure
4bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant//
5b64f8b07c104c6cc986570ac8ee0ed16a9f23976Howard Hinnant// This file is dual licensed under the MIT and the University of Illinois Open
6b64f8b07c104c6cc986570ac8ee0ed16a9f23976Howard Hinnant// Source Licenses. See LICENSE.TXT for details.
7bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant//
8bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant//===----------------------------------------------------------------------===//
97112dae6acac544a0271a85d95342c583441e2d1Dan Albert//
107112dae6acac544a0271a85d95342c583441e2d1Dan Albert// UNSUPPORTED: libcpp-has-no-threads
11bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant
12bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant// <mutex>
13bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant
14bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant// template <class Mutex> class lock_guard;
15bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant
16bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant// explicit lock_guard(mutex_type& m);
17bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant
18bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant#include <mutex>
19bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant#include <thread>
20bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant#include <cstdlib>
21bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant#include <cassert>
22bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant
23bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnantstd::mutex m;
24bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant
25bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnanttypedef std::chrono::system_clock Clock;
26bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnanttypedef Clock::time_point time_point;
27bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnanttypedef Clock::duration duration;
28bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnanttypedef std::chrono::milliseconds ms;
29bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnanttypedef std::chrono::nanoseconds ns;
30bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant
31bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnantvoid f()
32bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant{
33bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant    time_point t0 = Clock::now();
34bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant    time_point t1;
35bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant    {
36bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant    std::lock_guard<std::mutex> lg(m);
37bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant    t1 = Clock::now();
38bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant    }
39bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant    ns d = t1 - t0 - ms(250);
40fe14b970ac1ce90da0d22aa0b83fc48298d94327Daniel Dunbar    assert(d < ms(200));  // within 200ms
41bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant}
42bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant
43bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnantint main()
44bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant{
45bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant    m.lock();
46bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant    std::thread t(f);
47bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant    std::this_thread::sleep_for(ms(250));
48bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant    m.unlock();
49bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant    t.join();
50bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant}
51