libstdc++
this_thread_sleep.h
Go to the documentation of this file.
1// std::this_thread::sleep_for/until declarations -*- C++ -*-
2
3// Copyright (C) 2008-2025 Free Software Foundation, Inc.
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
24
25/** @file bits/this_thread_sleep.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{thread}
28 */
29
30#ifndef _GLIBCXX_THIS_THREAD_SLEEP_H
31#define _GLIBCXX_THIS_THREAD_SLEEP_H 1
32
33#ifdef _GLIBCXX_SYSHDR
34#pragma GCC system_header
35#endif
36
37#if __cplusplus >= 201103L
38#include <bits/chrono.h> // std::chrono::*
39#include <ext/numeric_traits.h> // __int_traits
40
41#ifdef _GLIBCXX_USE_NANOSLEEP
42# include <cerrno> // errno, EINTR
43# include <time.h> // nanosleep
44#endif
45
46namespace std _GLIBCXX_VISIBILITY(default)
47{
48_GLIBCXX_BEGIN_NAMESPACE_VERSION
49
50 /** @addtogroup threads
51 * @{
52 */
53
54 /** @namespace std::this_thread
55 * @brief ISO C++ 2011 namespace for interacting with the current thread
56 *
57 * C++11 30.3.2 [thread.thread.this] Namespace this_thread.
58 */
59 namespace this_thread
60 {
61#ifndef _GLIBCXX_NO_SLEEP
62
63 /// this_thread::sleep_for
64 template<typename _Rep, typename _Period>
65 inline void
67 {
68 if (__rtime <= __rtime.zero())
69 return;
70
71 struct timespec __ts = chrono::__to_timeout_timespec(__rtime);
72#ifdef _GLIBCXX_USE_NANOSLEEP
73 while (::nanosleep(&__ts, &__ts) == -1 && errno == EINTR)
74 { }
75#else
76 using chrono::seconds;
78 void __sleep_for(seconds __s, nanoseconds __ns);
79 __sleep_for(seconds(__ts.tv_sec), nanoseconds(__ts.tv_nsec));
80#endif
81 }
82
83 /// this_thread::sleep_until
84 template<typename _Clock, typename _Duration>
85 inline void
87 {
88#if __cplusplus > 201703L
89 static_assert(chrono::is_clock_v<_Clock>);
90#endif
91 auto __now = _Clock::now();
92 if (_Clock::is_steady)
93 {
94 if (__now < __atime)
95 sleep_for(__atime - __now);
96 return;
97 }
98 while (__now < __atime)
99 {
100 sleep_for(__atime - __now);
101 __now = _Clock::now();
102 }
103 }
104#endif // ! NO_SLEEP
105 } // namespace this_thread
106
107 /// @}
108
109_GLIBCXX_END_NAMESPACE_VERSION
110} // namespace
111#endif // C++11
112
113#endif // _GLIBCXX_THIS_THREAD_SLEEP_H
duration< int64_t, nano > nanoseconds
nanoseconds
Definition chrono.h:892
duration< int64_t > seconds
seconds
Definition chrono.h:901
ISO C++ entities toplevel namespace is std.
ISO C++ 2011 namespace for interacting with the current thread.
Definition std_thread.h:358
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
Definition chrono.h:516
chrono::time_point represents a point in time as measured by a clock
Definition chrono.h:927