Intel(R) Threading Building Blocks Doxygen Documentation version 4.2.3
tbb_thread.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2005-2020 Intel Corporation
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15*/
16
18
19#if !defined(__TBB_show_deprecation_message_tbb_thread_H) && defined(__TBB_show_deprecated_header_message)
20#define __TBB_show_deprecation_message_tbb_thread_H
21#pragma message("TBB Warning: tbb/tbb_thread.h is deprecated. For details, please see Deprecated Features appendix in the TBB reference manual.")
22#endif
23
24#if defined(__TBB_show_deprecated_header_message)
25#undef __TBB_show_deprecated_header_message
26#endif
27
28#ifndef __TBB_tbb_thread_H
29#define __TBB_tbb_thread_H
30
31#define __TBB_tbb_thread_H_include_area
33
34#include "tbb_stddef.h"
35
36#if _WIN32||_WIN64
37#include "machine/windows_api.h"
38#define __TBB_NATIVE_THREAD_ROUTINE unsigned WINAPI
39#define __TBB_NATIVE_THREAD_ROUTINE_PTR(r) unsigned (WINAPI* r)( void* )
40namespace tbb { namespace internal {
41#if __TBB_WIN8UI_SUPPORT
42 typedef size_t thread_id_type;
43#else // __TBB_WIN8UI_SUPPORT
44 typedef DWORD thread_id_type;
45#endif // __TBB_WIN8UI_SUPPORT
46}} //namespace tbb::internal
47#else
48#define __TBB_NATIVE_THREAD_ROUTINE void*
49#define __TBB_NATIVE_THREAD_ROUTINE_PTR(r) void* (*r)( void* )
50#include <pthread.h>
51namespace tbb { namespace internal {
52 typedef pthread_t thread_id_type;
53}} //namespace tbb::internal
54#endif // _WIN32||_WIN64
55
56#include "atomic.h"
58#include "tick_count.h"
59
60#include __TBB_STD_SWAP_HEADER
61#include <iosfwd>
62
63namespace tbb {
64
65namespace internal {
66 class tbb_thread_v3;
67}
68
69inline void swap( internal::tbb_thread_v3& t1, internal::tbb_thread_v3& t2 ) __TBB_NOEXCEPT(true);
70
71namespace internal {
72
77
79 void* operator new( size_t size ) {return allocate_closure_v3(size);}
80 void operator delete( void* ptr ) {free_closure_v3(ptr);}
81 };
82
83 template<class F> struct thread_closure_0: thread_closure_base {
85
87 thread_closure_0 *self = static_cast<thread_closure_0*>(c);
88 self->function();
89 delete self;
90 return 0;
91 }
92 thread_closure_0( const F& f ) : function(f) {}
93 };
95 template<class F, class X> struct thread_closure_1: thread_closure_base {
100 thread_closure_1 *self = static_cast<thread_closure_1*>(c);
101 self->function(self->arg1);
102 delete self;
103 return 0;
104 }
105 thread_closure_1( const F& f, const X& x ) : function(f), arg1(x) {}
106 };
107 template<class F, class X, class Y> struct thread_closure_2: thread_closure_base {
113 thread_closure_2 *self = static_cast<thread_closure_2*>(c);
114 self->function(self->arg1, self->arg2);
115 delete self;
116 return 0;
117 }
118 thread_closure_2( const F& f, const X& x, const Y& y ) : function(f), arg1(x), arg2(y) {}
119 };
120
123#if __TBB_IF_NO_COPY_CTOR_MOVE_SEMANTICS_BROKEN
124 // Workaround for a compiler bug: declaring the copy constructor as public
125 // enables use of the moving constructor.
126 // The definition is not provided in order to prohibit copying.
127 public:
128#endif
129 tbb_thread_v3(const tbb_thread_v3&); // = delete; // Deny access
130 public:
131#if _WIN32||_WIN64
132 typedef HANDLE native_handle_type;
133#else
134 typedef pthread_t native_handle_type;
135#endif // _WIN32||_WIN64
136
137 class id;
140#if _WIN32||_WIN64
141 , my_thread_id(0)
142#endif // _WIN32||_WIN64
143 {}
144
146 template <class F> explicit tbb_thread_v3(F f) {
147 typedef internal::thread_closure_0<F> closure_type;
148 internal_start(closure_type::start_routine, new closure_type(f));
149 }
151 template <class F, class X> tbb_thread_v3(F f, X x) {
152 typedef internal::thread_closure_1<F,X> closure_type;
153 internal_start(closure_type::start_routine, new closure_type(f,x));
154 }
156 template <class F, class X, class Y> tbb_thread_v3(F f, X x, Y y) {
157 typedef internal::thread_closure_2<F,X,Y> closure_type;
158 internal_start(closure_type::start_routine, new closure_type(f,x,y));
159 }
160
161#if __TBB_CPP11_RVALUE_REF_PRESENT
164#if _WIN32||_WIN64
165 , my_thread_id(x.my_thread_id)
166#endif
167 {
168 x.internal_wipe();
169 }
171 internal_move(x);
172 return *this;
173 }
174 private:
175 tbb_thread_v3& operator=(const tbb_thread_v3& x); // = delete;
176 public:
177#else // __TBB_CPP11_RVALUE_REF_PRESENT
179 internal_move(x);
180 return *this;
181 }
182#endif // __TBB_CPP11_RVALUE_REF_PRESENT
183
184 void swap( tbb_thread_v3& t ) __TBB_NOEXCEPT(true) {tbb::swap( *this, t );}
185 bool joinable() const __TBB_NOEXCEPT(true) {return my_handle!=0; }
191 inline id get_id() const __TBB_NOEXCEPT(true);
193
195
205 private:
207#if _WIN32||_WIN64
208 thread_id_type my_thread_id;
209#endif // _WIN32||_WIN64
210
212 my_handle = 0;
213#if _WIN32||_WIN64
214 my_thread_id = 0;
215#endif
216 }
218 if (joinable()) detach();
219 my_handle = x.my_handle;
220#if _WIN32||_WIN64
221 my_thread_id = x.my_thread_id;
222#endif // _WIN32||_WIN64
223 x.internal_wipe();
224 }
225
228 void* closure );
231 };
232
235 id( thread_id_type id_ ) : my_id(id_) {}
236
237 friend class tbb_thread_v3;
238 public:
239 id() __TBB_NOEXCEPT(true) : my_id(0) {}
240
247
248 template<class charT, class traits>
249 friend std::basic_ostream<charT, traits>&
250 operator<< (std::basic_ostream<charT, traits> &out,
252 {
253 out << id.my_id;
254 return out;
255 }
257
258 friend inline size_t tbb_hasher( const tbb_thread_v3::id& id ) {
259 __TBB_STATIC_ASSERT(sizeof(id.my_id) <= sizeof(size_t), "Implementation assumes that thread_id_type fits into machine word");
260 return tbb::tbb_hasher(id.my_id);
261 }
262
263 // A workaround for lack of tbb::atomic<id> (which would require id to be POD in C++03).
264 friend id atomic_compare_and_swap(id& location, const id& value, const id& comparand){
265 return as_atomic(location.my_id).compare_and_swap(value.my_id, comparand.my_id);
266 }
267 }; // tbb_thread_v3::id
268
270#if _WIN32||_WIN64
271 return id(my_thread_id);
272#else
273 return id(my_handle);
274#endif // _WIN32||_WIN64
275 }
276
281
283 {
284 return x.my_id == y.my_id;
285 }
287 {
288 return x.my_id != y.my_id;
289 }
291 {
292 return x.my_id < y.my_id;
293 }
295 {
296 return x.my_id <= y.my_id;
297 }
299 {
300 return x.my_id > y.my_id;
301 }
303 {
304 return x.my_id >= y.my_id;
305 }
306
307} // namespace internal;
308
310__TBB_DEPRECATED_IN_VERBOSE_MODE_MSG("tbb::thread is deprecated, use std::thread") typedef internal::tbb_thread_v3 tbb_thread;
311
312using internal::operator==;
313using internal::operator!=;
314using internal::operator<;
315using internal::operator>;
316using internal::operator<=;
317using internal::operator>=;
318
319inline void move( tbb_thread& t1, tbb_thread& t2 ) {
320 internal::move_v3(t1, t2);
321}
322
324 std::swap(t1.my_handle, t2.my_handle);
325#if _WIN32||_WIN64
326 std::swap(t1.my_thread_id, t2.my_thread_id);
327#endif /* _WIN32||_WIN64 */
328}
329
330namespace this_tbb_thread {
337 }
338} // namespace this_tbb_thread
339
340} // namespace tbb
341
343#undef __TBB_tbb_thread_H_include_area
344
345#endif /* __TBB_tbb_thread_H */
#define __TBB_NOEXCEPT(expression)
Definition: tbb_stddef.h:110
#define __TBB_EXPORTED_METHOD
Definition: tbb_stddef.h:98
#define __TBB_STATIC_ASSERT(condition, msg)
Definition: tbb_stddef.h:553
#define __TBB_NATIVE_THREAD_ROUTINE
Definition: tbb_thread.h:48
#define __TBB_NATIVE_THREAD_ROUTINE_PTR(r)
Definition: tbb_thread.h:49
#define __TBB_DEPRECATED_IN_VERBOSE_MODE
Definition: tbb_config.h:647
#define __TBB_EXPORTED_FUNC
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t ITT_FORMAT d void ITT_FORMAT p void ITT_FORMAT p __itt_model_site __itt_model_site_instance ITT_FORMAT p __itt_model_task __itt_model_task_instance ITT_FORMAT p void ITT_FORMAT p void ITT_FORMAT p void size_t ITT_FORMAT d void ITT_FORMAT p const wchar_t ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s no args void ITT_FORMAT p size_t ITT_FORMAT d no args const wchar_t const wchar_t ITT_FORMAT s __itt_heap_function void size_t int ITT_FORMAT d __itt_heap_function void ITT_FORMAT p __itt_heap_function void void size_t int ITT_FORMAT d no args no args unsigned int ITT_FORMAT u const __itt_domain __itt_id ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain __itt_id ITT_FORMAT p const __itt_domain __itt_id __itt_timestamp __itt_timestamp ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain ITT_FORMAT p const __itt_domain __itt_string_handle unsigned long long value
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t size
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t ITT_FORMAT d void ITT_FORMAT p void ITT_FORMAT p __itt_model_site __itt_model_site_instance ITT_FORMAT p __itt_model_task __itt_model_task_instance ITT_FORMAT p void ITT_FORMAT p void ITT_FORMAT p void size_t ITT_FORMAT d void ITT_FORMAT p const wchar_t ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s no args void ITT_FORMAT p size_t ITT_FORMAT d no args const wchar_t const wchar_t ITT_FORMAT s __itt_heap_function void size_t int ITT_FORMAT d __itt_heap_function void ITT_FORMAT p __itt_heap_function void void size_t int ITT_FORMAT d no args no args unsigned int ITT_FORMAT u const __itt_domain __itt_id id
#define private
class __TBB_DEPRECATED_IN_VERBOSE_MODE_MSG("tbb::aligned_space is deprecated, use std::aligned_storage") aligned_space
Block of space aligned sufficiently to construct an array T with N elements.
Definition: aligned_space.h:43
The graph class.
void swap(concurrent_hash_map< Key, T, HashCompare, A > &a, concurrent_hash_map< Key, T, HashCompare, A > &b)
void swap(internal::tbb_thread_v3 &t1, internal::tbb_thread_v3 &t2) __TBB_NOEXCEPT(true)
Definition: tbb_thread.h:323
void move(tbb_thread &t1, tbb_thread &t2)
Definition: tbb_thread.h:319
bool operator>=(const vector_iterator< Container, T > &i, const vector_iterator< Container, U > &j)
void __TBB_EXPORTED_FUNC free_closure_v3(void *)
Free a closure allocated by allocate_closure_v3.
Definition: tbb_thread.cpp:41
void __TBB_EXPORTED_FUNC thread_yield_v3()
Definition: tbb_thread.cpp:159
bool operator!=(const vector_iterator< Container, T > &i, const vector_iterator< Container, U > &j)
bool operator==(const vector_iterator< Container, T > &i, const vector_iterator< Container, U > &j)
bool operator<=(const vector_iterator< Container, T > &i, const vector_iterator< Container, U > &j)
tbb_thread_v3::id __TBB_EXPORTED_FUNC thread_get_id_v3()
Definition: tbb_thread.cpp:139
void *__TBB_EXPORTED_FUNC allocate_closure_v3(size_t size)
Allocate a closure.
Definition: tbb_thread.cpp:35
atomic< T > & as_atomic(T &t)
Definition: atomic.h:572
bool operator>(const vector_iterator< Container, T > &i, const vector_iterator< Container, U > &j)
pthread_t thread_id_type
Definition: tbb_thread.h:52
void __TBB_EXPORTED_FUNC move_v3(tbb_thread_v3 &t1, tbb_thread_v3 &t2)
Definition: tbb_thread.cpp:147
void __TBB_EXPORTED_FUNC thread_sleep_v3(const tick_count::interval_t &i)
Definition: tbb_thread.cpp:164
bool operator<(const vector_iterator< Container, T > &i, const vector_iterator< Container, U > &j)
__TBB_DEPRECATED_IN_VERBOSE_MODE tbb_thread::id get_id()
Definition: tbb_thread.h:331
__TBB_DEPRECATED_IN_VERBOSE_MODE void yield()
Offers the operating system the opportunity to schedule another thread.
Definition: tbb_thread.h:333
__TBB_DEPRECATED_IN_VERBOSE_MODE void sleep(const tick_count::interval_t &i)
The current thread blocks at least until the time specified.
Definition: tbb_thread.h:335
static __TBB_NATIVE_THREAD_ROUTINE start_routine(void *c)
Definition: tbb_thread.h:86
Structure used to pass user function with 1 argument to thread.
Definition: tbb_thread.h:95
static __TBB_NATIVE_THREAD_ROUTINE start_routine(void *c)
Routine passed to Windows's _beginthreadex by thread::internal_start() inside tbb....
Definition: tbb_thread.h:99
thread_closure_1(const F &f, const X &x)
Definition: tbb_thread.h:105
static __TBB_NATIVE_THREAD_ROUTINE start_routine(void *c)
Routine passed to Windows's _beginthreadex by thread::internal_start() inside tbb....
Definition: tbb_thread.h:112
thread_closure_2(const F &f, const X &x, const Y &y)
Definition: tbb_thread.h:118
Versioned thread class.
Definition: tbb_thread.h:122
tbb_thread_v3() __TBB_NOEXCEPT(true)
Constructs a thread object that does not represent a thread of execution.
Definition: tbb_thread.h:139
native_handle_type my_handle
Definition: tbb_thread.h:206
void internal_wipe() __TBB_NOEXCEPT(true)
Definition: tbb_thread.h:211
bool joinable() const __TBB_NOEXCEPT(true)
Definition: tbb_thread.h:185
tbb_thread_v3(tbb_thread_v3 &&x) __TBB_NOEXCEPT(true)
Definition: tbb_thread.h:162
tbb_thread_v3(F f, X x)
Constructs an object and executes f(x) in a new thread.
Definition: tbb_thread.h:151
void internal_move(tbb_thread_v3 &x) __TBB_NOEXCEPT(true)
Definition: tbb_thread.h:217
void __TBB_EXPORTED_METHOD internal_start(__TBB_NATIVE_THREAD_ROUTINE_PTR(start_routine), void *closure)
Definition: tbb_thread.cpp:90
static unsigned __TBB_EXPORTED_FUNC hardware_concurrency() __TBB_NOEXCEPT(true)
The number of hardware thread contexts.
Definition: tbb_thread.cpp:135
native_handle_type native_handle()
Definition: tbb_thread.h:192
friend void __TBB_EXPORTED_FUNC move_v3(tbb_thread_v3 &t1, tbb_thread_v3 &t2)
Definition: tbb_thread.cpp:147
tbb_thread_v3 & operator=(tbb_thread_v3 &&x) __TBB_NOEXCEPT(true)
Definition: tbb_thread.h:170
tbb_thread_v3(F f)
Constructs an object and executes f() in a new thread.
Definition: tbb_thread.h:146
void swap(tbb_thread_v3 &t) __TBB_NOEXCEPT(true)
Definition: tbb_thread.h:184
void __TBB_EXPORTED_METHOD join()
The completion of the thread represented by *this happens before join() returns.
Definition: tbb_thread.cpp:46
tbb_thread_v3(F f, X x, Y y)
Constructs an object and executes f(x,y) in a new thread.
Definition: tbb_thread.h:156
tbb_thread_v3(const tbb_thread_v3 &)
tbb_thread_v3 & operator=(const tbb_thread_v3 &x)
id get_id() const __TBB_NOEXCEPT(true)
Definition: tbb_thread.h:269
void __TBB_EXPORTED_METHOD detach()
When detach() returns, *this no longer represents the possibly continuing thread of execution.
Definition: tbb_thread.cpp:74
friend tbb_thread_v3::id __TBB_EXPORTED_FUNC thread_get_id_v3()
Definition: tbb_thread.cpp:139
friend bool operator==(tbb_thread_v3::id x, tbb_thread_v3::id y) __TBB_NOEXCEPT(true)
Definition: tbb_thread.h:282
friend bool operator!=(tbb_thread_v3::id x, tbb_thread_v3::id y) __TBB_NOEXCEPT(true)
Definition: tbb_thread.h:286
friend bool operator<(tbb_thread_v3::id x, tbb_thread_v3::id y) __TBB_NOEXCEPT(true)
Definition: tbb_thread.h:290
id() __TBB_NOEXCEPT(true)
Definition: tbb_thread.h:239
friend size_t tbb_hasher(const tbb_thread_v3::id &id)
Definition: tbb_thread.h:258
friend bool operator>=(tbb_thread_v3::id x, tbb_thread_v3::id y) __TBB_NOEXCEPT(true)
Definition: tbb_thread.h:302
friend bool operator<=(tbb_thread_v3::id x, tbb_thread_v3::id y) __TBB_NOEXCEPT(true)
Definition: tbb_thread.h:294
friend std::basic_ostream< charT, traits > & operator<<(std::basic_ostream< charT, traits > &out, tbb_thread_v3::id id)
Definition: tbb_thread.h:250
friend bool operator>(tbb_thread_v3::id x, tbb_thread_v3::id y) __TBB_NOEXCEPT(true)
Definition: tbb_thread.h:298
friend id atomic_compare_and_swap(id &location, const id &value, const id &comparand)
Definition: tbb_thread.h:264
Relative time interval.
Definition: tick_count.h:37

Copyright © 2005-2020 Intel Corporation. All Rights Reserved.

Intel, Pentium, Intel Xeon, Itanium, Intel XScale and VTune are registered trademarks or trademarks of Intel Corporation or its subsidiaries in the United States and other countries.

* Other names and brands may be claimed as the property of others.