carb::thread::ThreadLocal< T, true >¶
Defined in carb/thread/ThreadLocal.h
Inheritance Relationships¶
Base Type¶
private ThreadLocalBase
-
template<class
T
>
classcarb::thread
::
ThreadLocal
<T, true> : private ThreadLocalBase¶ A class for declaring a dynamic thread-local variable (Trivial/Pointer/POD specialization).
This is necessary since C++11 the
thread_local
storage class specifier can only be used at namespace scope. There is no way to declare athread_local
variable at class scope unless it is static. ThreadLocal is dynamic.There are two specializations for ThreadLocal: a trivial version and a non-trivial version. The trivial version is designed for types like plain-old-data that are pointer-sized or less. The non-trivial version supports classes and large types. The heap is used for non-trivial ThreadLocal types, however these are lazy-initialize, so the per-thread memory is only allocated when used for the first time. The type is automatically destructed (and the memory returned to the heap) when a thread exits.
Note
Most systems have a limit to the number of thread-local variables available. Each instance of ThreadLocal will consume some of that storage. Therefore, if a class contains a non-static ThreadLocal member, each instance of that class will consume another slot. Use sparingly.
On Windows, this class is implemented using Thread Local Storage APIs. On Linux, this class is implemented using pthread_key_t.
Public Functions
-
inline
ThreadLocal
()¶ Constructor.
Allocates a thread-local storage slot from the operating system.
-
~ThreadLocal
() = default¶ Destructor.
Returns the previously allocated thread-local storage slot to the operating system.
-
inline T
get
() const¶ Returns the specific value of this ThreadLocal variable for this thread.
Note
If the calling thread has not yet set() a value for this thread-local storage, the value returned will be default-initialized. For POD types this will be initialized to zeros.
-
inline void
set
(T t)¶ Sets the specific value of this ThreadLocal variable for this thread.
- Parameters
t – The specific value to set for this thread.
-
inline ThreadLocal &
operator=
(T t)¶ Assignment operator and alias for set().
- Returns
*this
-
inline T
operator->
() const¶ For types where
T
is a pointer, allows dereferencing the thread-local value.- Returns
The value as if by get().
-
inline auto
operator*
() const¶ For types where
T
is a pointer, allows dereferencing the thread-local value.- Returns
The value as if by set().
-
inline