Simbody  3.6
SimTK::ThreadLocal< T > Class Template Reference

(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...
 

Detailed Description

template<class T>
class SimTK::ThreadLocal< T >

(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<int> x;
...
x.upd() = 5;
assert(x.get() == 5);
Warning
You should avoid allocating 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.

Constructor & Destructor Documentation

template<class T >
SimTK::ThreadLocal< T >::ThreadLocal ( )
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.

template<class T >
SimTK::ThreadLocal< T >::ThreadLocal ( const T &  defaultValue)
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.

Parameters
defaultValueThe initial value that the objects of type T will have when created by the individual threads.
template<class T >
SimTK::ThreadLocal< T >::~ThreadLocal ( )
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.

Member Function Documentation

template<class T >
T& SimTK::ThreadLocal< T >::upd ( )
inline

Get a writable reference to the value of type T that was allocated for the current thread's exclusive use.

template<class T >
const T& SimTK::ThreadLocal< T >::get ( ) const
inline

Get a const reference to the value of type T that was allocated for the current thread's exclusive use.


The documentation for this class was generated from the following file: