Simbody
3.6
|
(Deprecated) This class represents a "thread local" variable: one which may have a different value on each thread; use C++11 thread_local
instead.
More...
Public Member Functions | |
ThreadLocal () | |
Create a new ThreadLocal<T> object. More... | |
ThreadLocal (const T &defaultValue) | |
Create a new ThreadLocal<T> object and provide a default value of type T to be used to initialize the thread-local objects when they are first allocated by the individual threads. More... | |
~ThreadLocal () | |
Destructor deletes the thread local object but does not delete the individual thread-allocated objects of type T. More... | |
T & | upd () |
Get a writable reference to the value of type T that was allocated for the current thread's exclusive use. More... | |
const T & | get () const |
Get a const reference to the value of type T that was allocated for the current thread's exclusive use. More... | |
(Deprecated) This class represents a "thread local" variable: one which may have a different value on each thread; use C++11 thread_local
instead.
This class is no longer necessary since C++11 has thread_local
as a built-in keyword – you should use that instead.
Thread-local storage is useful in many situations when writing multithreaded code. For example, it can be used as temporary workspace for calculations. If a single workspace object were created, all access to it would need to be synchronized to prevent threads from overwriting each other's values. Using a ThreadLocal<T>
instead means that a separate workspace object of type T
will automatically be created for each thread. That object will have "thread scope" meaning it will be destructed only on thread termination. Note that that means it can outlive destruction of the ThreadLocal<T>
object.
To use it, simply create a ThreadLocal<T>
, then call get() or upd() to get a readable or writable reference to the value of type T
that is available for the exclusive use of the current thread:
ThreadLocal
objects in single-threaded code because the objects of type T
have "thread scope"; they do not get destructed when the ThreadLocal
object does. So in the single-threaded case they will persist until program termination, creating a potential for leaks.
|
inline |
Create a new ThreadLocal<T>
object.
This does not allocate any of the thread-local objects of type T
; that is done from the individual threads when they first request such an object.
|
inlineexplicit |
Create a new ThreadLocal<T>
object and provide a default value of type T
to be used to initialize the thread-local objects when they are first allocated by the individual threads.
defaultValue | The initial value that the objects of type T will have when created by the individual threads. |
|
inline |
Destructor deletes the thread local object but does not delete the individual thread-allocated objects of type T.
Those are deleted only on thread termination.
|
inline |
Get a writable reference to the value of type T
that was allocated for the current thread's exclusive use.
|
inline |
Get a const reference to the value of type T
that was allocated for the current thread's exclusive use.