xref: /llvm-project/libcxx/test/std/thread/thread.threads/thread.thread.this/sleep_until.pass.cpp (revision 7fc6a55688c816f5fc1a5481ae7af25be7500356)
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: libcpp-has-no-threads
10 // FLAKY_TEST.
11 
12 // <thread>
13 
14 // template <class Clock, class Duration>
15 //   void sleep_until(const chrono::time_point<Clock, Duration>& abs_time);
16 
17 #include <thread>
18 #include <cstdlib>
19 #include <cassert>
20 
21 #include "test_macros.h"
22 
23 int main(int, char**)
24 {
25     typedef std::chrono::system_clock Clock;
26     typedef Clock::time_point time_point;
27     std::chrono::milliseconds ms(500);
28     time_point t0 = Clock::now();
29     std::this_thread::sleep_until(t0 + ms);
30     time_point t1 = Clock::now();
31     std::chrono::nanoseconds ns = (t1 - t0) - ms;
32     std::chrono::nanoseconds err = 5 * ms / 100;
33     // The time slept is within 5% of 500ms
34     assert(std::abs(ns.count()) < err.count());
35 
36   return 0;
37 }
38