libstdc++: this_thread_sleep.h Source File

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-2026 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
40#ifdef _GLIBCXX_USE_NANOSLEEP
41# include <cerrno> // errno, EINTR
42# include <time.h> // nanosleep
43#endif
44
45namespace std _GLIBCXX_VISIBILITY(default)
46{
47_GLIBCXX_BEGIN_NAMESPACE_VERSION
48
49 /** @addtogroup threads
50 * @{
51 */
52
53 /** @namespace std::this_thread
54 * @brief ISO C++ 2011 namespace for interacting with the current thread
55 *
56 * C++11 30.3.2 [thread.thread.this] Namespace this_thread.
57 */
58 namespace this_thread
59 {
60#ifndef _GLIBCXX_NO_SLEEP
61
62 /// this_thread::sleep_for
63 template<typename _Rep, typename _Period>
64 inline void
65 sleep_for(const chrono::duration<_Rep, _Period>& __rtime)
66 {
67 if (__rtime <= __rtime.zero())
68 return;
69
70 struct timespec __ts = chrono::__to_timeout_timespec(__rtime);
71#ifdef _GLIBCXX_USE_NANOSLEEP
72 while (::nanosleep(&__ts, &__ts) == -1 && errno == EINTR)
73 { }
74#else
75 using chrono::seconds;
76 using chrono::nanoseconds;
77 void __sleep_for(seconds __s, nanoseconds __ns);
78 __sleep_for(seconds(__ts.tv_sec), nanoseconds(__ts.tv_nsec));
79#endif
80 }
81
82 /// this_thread::sleep_until
83 template<typename _Clock, typename _Duration>
84 inline void
85 sleep_until(const chrono::time_point<_Clock, _Duration>& __atime)
86 {
87#if __cplusplus > 201703L
88 static_assert(chrono::is_clock_v<_Clock>);
89#endif
90 auto __now = _Clock::now();
91 if (_Clock::is_steady)
92 {
93 if (__now < __atime)
94 sleep_for(__atime - __now);
95 return;
96 }
97 while (__now < __atime)
98 {
99 sleep_for(__atime - __now);
100 __now = _Clock::now();
101 }
102 }
103#endif // ! NO_SLEEP
104 } // namespace this_thread
105
106 /// @}
107
108_GLIBCXX_END_NAMESPACE_VERSION
109} // namespace
110#endif // C++11
111
112#endif // _GLIBCXX_THIS_THREAD_SLEEP_H
cerrno
chrono.h
std::chrono::nanoseconds
duration< int64_t, nano > nanoseconds
nanoseconds
Definition chrono.h:896
std::chrono::seconds
duration< int64_t > seconds
seconds
Definition chrono.h:905
std
ISO C++ entities toplevel namespace is std.
std::this_thread
ISO C++ 2011 namespace for interacting with the current thread.
Definition std_thread.h:358
std::this_thread::sleep_until
void sleep_until(const chrono::time_point< _Clock, _Duration > &__atime)
this_thread::sleep_until
Definition this_thread_sleep.h:85
std::this_thread::sleep_for
void sleep_for(const chrono::duration< _Rep, _Period > &__rtime)
this_thread::sleep_for
Definition this_thread_sleep.h:65
std::chrono::duration
chrono::duration represents a distance between two points in time
Definition chrono.h:516
std::chrono::time_point
chrono::time_point represents a point in time as measured by a clock
Definition chrono.h:931