Simbody  3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
SimTK::ThreadLocal< T > Class Template Reference

This class represents a "thread local" variable: one which has a different value on each thread. More...

Public Member Functions

 ThreadLocal ()
 Create a new ThreadLocal variable. More...
 
 ThreadLocal (const T &defaultValue)
 Create a new ThreadLocal variable. More...
 
 ~ThreadLocal ()
 
T & upd ()
 Get a reference to the value for the current thread. More...
 
const T & get () const
 Get a const reference to the value for the current thread. More...
 

Detailed Description

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

This class represents a "thread local" variable: one which has a different value on each thread.

This 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 instead means that a separate workspace object will automatically be created for each thread.

To use it, simply create a ThreadLocal, then call get() or upd() to get a readable or writable reference to the value for the current thread:

ThreadLocal<int> x;
...
x.upd() = 5;
assert(x.get() == 5);

Constructor & Destructor Documentation

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

Create a new ThreadLocal variable.

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

Create a new ThreadLocal variable.

Parameters
defaultValuethe initial value which the variable will have on each thread
template<class T >
SimTK::ThreadLocal< T >::~ThreadLocal ( )
inline

Member Function Documentation

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

Get a reference to the value for the current thread.

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

Get a const reference to the value for the current thread.


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