libstdc++
thread
Go to the documentation of this file.
1// <thread> -*- C++ -*-
2
3// Copyright (C) 2008-2016 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 include/thread
26 * This is a Standard C++ Library header.
27 */
28
29#ifndef _GLIBCXX_THREAD
30#define _GLIBCXX_THREAD 1
31
32#pragma GCC system_header
33
34#if __cplusplus < 201103L
35# include <bits/c++0x_warning.h>
36#else
37
38#include <chrono>
39#include <functional>
40#include <memory>
41#include <cerrno>
42#include <bits/functexcept.h>
43#include <bits/functional_hash.h>
44#include <bits/gthr.h>
45
46#if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)
47
48namespace std _GLIBCXX_VISIBILITY(default)
49{
50_GLIBCXX_BEGIN_NAMESPACE_VERSION
51
52 /**
53 * @defgroup threads Threads
54 * @ingroup concurrency
55 *
56 * Classes for thread support.
57 * @{
58 */
59
60 /// thread
61 class thread
62 {
63 public:
64 // Abstract base class for types that wrap arbitrary functors to be
65 // invoked in the new thread of execution.
66 struct _State
67 {
68 virtual ~_State();
69 virtual void _M_run() = 0;
70 };
71 using _State_ptr = unique_ptr<_State>;
72
73 typedef __gthread_t native_handle_type;
74
75 /// thread::id
76 class id
77 {
78 native_handle_type _M_thread;
79
80 public:
81 id() noexcept : _M_thread() { }
82
83 explicit
84 id(native_handle_type __id) : _M_thread(__id) { }
85
86 private:
87 friend class thread;
88 friend class hash<thread::id>;
89
90 friend bool
91 operator==(thread::id __x, thread::id __y) noexcept
92 {
93 // pthread_equal is undefined if either thread ID is not valid, so we
94 // can't safely use __gthread_equal on default-constructed values (nor
95 // the non-zero value returned by this_thread::get_id() for
96 // single-threaded programs using GNU libc). Assume EqualityComparable.
97 return __x._M_thread == __y._M_thread;
98 }
99
100 friend bool
101 operator<(thread::id __x, thread::id __y) noexcept
102 {
103 // Pthreads doesn't define any way to do this, so we just have to
104 // assume native_handle_type is LessThanComparable.
105 return __x._M_thread < __y._M_thread;
106 }
107
108 template<class _CharT, class _Traits>
109 friend basic_ostream<_CharT, _Traits>&
110 operator<<(basic_ostream<_CharT, _Traits>& __out, thread::id __id);
111 };
112
113 private:
114 id _M_id;
115
116 public:
117 thread() noexcept = default;
118 // _GLIBCXX_RESOLVE_LIB_DEFECTS
119 // 2097. packaged_task constructors should be constrained
120 thread(thread&) = delete;
121 thread(const thread&) = delete;
122 thread(const thread&&) = delete;
123
124 thread(thread&& __t) noexcept
125 { swap(__t); }
126
127 template<typename _Callable, typename... _Args>
128 explicit
129 thread(_Callable&& __f, _Args&&... __args)
130 {
131#ifdef GTHR_ACTIVE_PROXY
132 // Create a reference to pthread_create, not just the gthr weak symbol.
133 auto __depend = reinterpret_cast<void(*)()>(&pthread_create);
134#else
135 auto __depend = nullptr;
136#endif
137 _M_start_thread(_S_make_state(
138 std::__bind_simple(std::forward<_Callable>(__f),
139 std::forward<_Args>(__args)...)),
140 __depend);
141 }
142
143 ~thread()
144 {
145 if (joinable())
146 std::terminate();
147 }
148
149 thread& operator=(const thread&) = delete;
150
151 thread& operator=(thread&& __t) noexcept
152 {
153 if (joinable())
154 std::terminate();
155 swap(__t);
156 return *this;
157 }
158
159 void
160 swap(thread& __t) noexcept
161 { std::swap(_M_id, __t._M_id); }
162
163 bool
164 joinable() const noexcept
165 { return !(_M_id == id()); }
166
167 void
168 join();
169
170 void
171 detach();
172
173 thread::id
174 get_id() const noexcept
175 { return _M_id; }
176
177 /** @pre thread is joinable
178 */
179 native_handle_type
180 native_handle()
181 { return _M_id._M_thread; }
182
183 // Returns a value that hints at the number of hardware thread contexts.
184 static unsigned int
185 hardware_concurrency() noexcept;
186
187 private:
188 template<typename _Callable>
189 struct _State_impl : public _State
190 {
191 _Callable _M_func;
192
193 _State_impl(_Callable&& __f) : _M_func(std::forward<_Callable>(__f))
194 { }
195
196 void
197 _M_run() { _M_func(); }
198 };
199
200 void
201 _M_start_thread(_State_ptr, void (*)());
202
203 template<typename _Callable>
204 static _State_ptr
205 _S_make_state(_Callable&& __f)
206 {
207 using _Impl = _State_impl<_Callable>;
208 return _State_ptr{new _Impl{std::forward<_Callable>(__f)}};
209 }
210#if _GLIBCXX_THREAD_ABI_COMPAT
211 public:
212 struct _Impl_base;
213 typedef shared_ptr<_Impl_base> __shared_base_type;
214 struct _Impl_base
215 {
216 __shared_base_type _M_this_ptr;
217 virtual ~_Impl_base() = default;
218 virtual void _M_run() = 0;
219 };
220
221 private:
222 void
223 _M_start_thread(__shared_base_type, void (*)());
224
225 void
226 _M_start_thread(__shared_base_type);
227#endif
228 };
229
230 inline void
231 swap(thread& __x, thread& __y) noexcept
232 { __x.swap(__y); }
233
234 inline bool
235 operator!=(thread::id __x, thread::id __y) noexcept
236 { return !(__x == __y); }
237
238 inline bool
239 operator<=(thread::id __x, thread::id __y) noexcept
240 { return !(__y < __x); }
241
242 inline bool
243 operator>(thread::id __x, thread::id __y) noexcept
244 { return __y < __x; }
245
246 inline bool
247 operator>=(thread::id __x, thread::id __y) noexcept
248 { return !(__x < __y); }
249
250 // DR 889.
251 /// std::hash specialization for thread::id.
252 template<>
253 struct hash<thread::id>
254 : public __hash_base<size_t, thread::id>
255 {
256 size_t
257 operator()(const thread::id& __id) const noexcept
258 { return std::_Hash_impl::hash(__id._M_thread); }
259 };
260
261 template<class _CharT, class _Traits>
262 inline basic_ostream<_CharT, _Traits>&
263 operator<<(basic_ostream<_CharT, _Traits>& __out, thread::id __id)
264 {
265 if (__id == thread::id())
266 return __out << "thread::id of a non-executing thread";
267 else
268 return __out << __id._M_thread;
269 }
270
271_GLIBCXX_END_NAMESPACE_VERSION
272
273 /** @namespace std::this_thread
274 * @brief ISO C++ 2011 entities sub-namespace for thread.
275 * 30.3.2 Namespace this_thread.
276 */
277 namespace this_thread
278 {
279 _GLIBCXX_BEGIN_NAMESPACE_VERSION
280
281 /// get_id
282 inline thread::id
283 get_id() noexcept
284 {
285#ifdef __GLIBC__
286 // For the GNU C library pthread_self() is usable without linking to
287 // libpthread.so but returns 0, so we cannot use it in single-threaded
288 // programs, because this_thread::get_id() != thread::id{} must be true.
289 // We know that pthread_t is an integral type in the GNU C library.
290 if (!__gthread_active_p())
291 return thread::id(1);
292#endif
293 return thread::id(__gthread_self());
294 }
295
296 /// yield
297 inline void
298 yield() noexcept
299 {
300#ifdef _GLIBCXX_USE_SCHED_YIELD
301 __gthread_yield();
302#endif
303 }
304
305 void
306 __sleep_for(chrono::seconds, chrono::nanoseconds);
307
308 /// sleep_for
309 template<typename _Rep, typename _Period>
310 inline void
311 sleep_for(const chrono::duration<_Rep, _Period>& __rtime)
312 {
313 if (__rtime <= __rtime.zero())
314 return;
315 auto __s = chrono::duration_cast<chrono::seconds>(__rtime);
316 auto __ns = chrono::duration_cast<chrono::nanoseconds>(__rtime - __s);
317#ifdef _GLIBCXX_USE_NANOSLEEP
318 __gthread_time_t __ts =
319 {
320 static_cast<std::time_t>(__s.count()),
321 static_cast<long>(__ns.count())
322 };
323 while (::nanosleep(&__ts, &__ts) == -1 && errno == EINTR)
324 { }
325#else
326 __sleep_for(__s, __ns);
327#endif
328 }
329
330 /// sleep_until
331 template<typename _Clock, typename _Duration>
332 inline void
333 sleep_until(const chrono::time_point<_Clock, _Duration>& __atime)
334 {
335 auto __now = _Clock::now();
336 if (_Clock::is_steady)
337 {
338 if (__now < __atime)
339 sleep_for(__atime - __now);
340 return;
341 }
342 while (__now < __atime)
343 {
344 sleep_for(__atime - __now);
345 __now = _Clock::now();
346 }
347 }
348
349 _GLIBCXX_END_NAMESPACE_VERSION
350 }
351
352 // @} group threads
353
354} // namespace
355
356#endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
357
358#endif // C++11
359
360#endif // _GLIBCXX_THREAD