1 //===----------------------------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // UNSUPPORTED: libcpp-has-no-threads 11 12 // <thread> 13 14 // template <class Rep, class Period> 15 // void sleep_for(const chrono::duration<Rep, Period>& rel_time); 16 17 #include <thread> 18 #include <cstdlib> 19 #include <cassert> 20 #include <cstring> 21 #include <signal.h> 22 #include <sys/time.h> 23 sig_action(int)24void sig_action(int) {} 25 main()26int main() 27 { 28 int ec; 29 struct sigaction action; 30 action.sa_handler = &sig_action; 31 sigemptyset(&action.sa_mask); 32 action.sa_flags = 0; 33 34 ec = sigaction(SIGALRM, &action, nullptr); 35 assert(!ec); 36 37 struct itimerval it; 38 std::memset(&it, 0, sizeof(itimerval)); 39 it.it_value.tv_sec = 0; 40 it.it_value.tv_usec = 250000; 41 // This will result in a SIGALRM getting fired resulting in the nanosleep 42 // inside sleep_for getting EINTR. 43 ec = setitimer(ITIMER_REAL, &it, nullptr); 44 assert(!ec); 45 46 typedef std::chrono::system_clock Clock; 47 typedef Clock::time_point time_point; 48 typedef Clock::duration duration; 49 std::chrono::milliseconds ms(500); 50 time_point t0 = Clock::now(); 51 std::this_thread::sleep_for(ms); 52 time_point t1 = Clock::now(); 53 std::chrono::nanoseconds ns = (t1 - t0) - ms; 54 std::chrono::nanoseconds err = 5 * ms / 100; 55 // The time slept is within 5% of 500ms 56 assert(std::abs(ns.count()) < err.count()); 57 } 58