xref: /llvm-project/libcxx/test/std/thread/thread.threads/thread.thread.this/sleep_until.pass.cpp (revision 57b08b0944046a6a57ee9b7b479181f548a5b9b4)
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 int main()
22 {
23     typedef std::chrono::system_clock Clock;
24     typedef Clock::time_point time_point;
25     std::chrono::milliseconds ms(500);
26     time_point t0 = Clock::now();
27     std::this_thread::sleep_until(t0 + ms);
28     time_point t1 = Clock::now();
29     std::chrono::nanoseconds ns = (t1 - t0) - ms;
30     std::chrono::nanoseconds err = 5 * ms / 100;
31     // The time slept is within 5% of 500ms
32     assert(std::abs(ns.count()) < err.count());
33 }
34