libstdc++: this_thread_sleep.h Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30#ifndef _GLIBCXX_THIS_THREAD_SLEEP_H
31#define _GLIBCXX_THIS_THREAD_SLEEP_H 1
32
33#pragma GCC system_header
34
35#if __cplusplus >= 201103L
37
38#ifdef _GLIBCXX_USE_NANOSLEEP
39# include <cerrno>
40# include <time.h>
41#endif
42
43namespace std _GLIBCXX_VISIBILITY(default)
44{
45_GLIBCXX_BEGIN_NAMESPACE_VERSION
46
47
48
49
50
51
52
53
54
55
56 namespace this_thread
57 {
58#ifndef _GLIBCXX_NO_SLEEP
59
60#ifndef _GLIBCXX_USE_NANOSLEEP
61 void
63#endif
64
65
66 template<typename _Rep, typename _Period>
67 inline void
69 {
70 if (__rtime <= __rtime.zero())
71 return;
72 auto __s = chrono::duration_castchrono::seconds(__rtime);
73 auto __ns = chrono::duration_castchrono::nanoseconds(__rtime - __s);
74#ifdef _GLIBCXX_USE_NANOSLEEP
75 struct ::timespec __ts =
76 {
77 static_caststd::time\_t\(__s.count()),
78 static_cast<long>(__ns.count())
79 };
80 while (::nanosleep(&__ts, &__ts) == -1 && errno == EINTR)
81 { }
82#else
83 __sleep_for(__s, __ns);
84#endif
85 }
86
87
88 template<typename _Clock, typename _Duration>
89 inline void
91 {
92#if __cplusplus > 201703L
93 static_assert(chrono::is_clock_v<_Clock>);
94#endif
95 auto __now = _Clock::now();
96 if (_Clock::is_steady)
97 {
98 if (__now < __atime)
100 return;
101 }
102 while (__now < __atime)
103 {
105 __now = _Clock::now();
106 }
107 }
108#endif
109 }
110
111
112
113_GLIBCXX_END_NAMESPACE_VERSION
114}
115#endif
116
117#endif
duration< int64_t > seconds
seconds
duration< int64_t, nano > nanoseconds
nanoseconds
ISO C++ entities toplevel namespace is std.
void sleep_until(const chrono::time_point< _Clock, _Duration > &__atime)
this_thread::sleep_until
void sleep_for(const chrono::duration< _Rep, _Period > &__rtime)
this_thread::sleep_for
chrono::duration represents a distance between two points in time
chrono::time_point represents a point in time as measured by a clock