1 #ifndef SimTK_SimTKCOMMON_THREAD_LOCAL_H_
2 #define SimTK_SimTKCOMMON_THREAD_LOCAL_H_
35 static pthread_mutex_t
keyLock = PTHREAD_MUTEX_INITIALIZER;
42 T* t =
reinterpret_cast<T*
>(value);
89 ThreadLocal(
const T& defaultValue) : defaultValue(defaultValue) {
96 pthread_key_delete(key);
103 for (std::set<void*>::const_iterator iter = instances.begin(); iter != instances.end(); ++iter) {
108 pthread_mutex_unlock(&
keyLock);
114 T* value =
reinterpret_cast<T*
>(pthread_getspecific(key));
116 return createValue();
122 const T&
get()
const {
123 T* value =
reinterpret_cast<T*
>(pthread_getspecific(key));
125 return createValue();
130 pthread_key_create(&key, cleanUpThreadLocalStorage<T>);
133 pthread_mutex_unlock(&
keyLock);
135 T& createValue()
const {
136 T* value =
new T(defaultValue);
137 pthread_setspecific(key, value);
141 pthread_mutex_unlock(&
keyLock);
150 #endif // SimTK_SimTKCOMMON_THREAD_LOCAL_H_
This class represents a "thread local" variable: one which has a different value on each thread...
Definition: ThreadLocal.h:76
T & upd()
Get a reference to the value for the current thread.
Definition: ThreadLocal.h:113
ThreadLocal(const T &defaultValue)
Create a new ThreadLocal variable.
Definition: ThreadLocal.h:89
static std::map< void *, pthread_key_t > instanceMap
Definition: ThreadLocal.h:33
static void cleanUpThreadLocalStorage(void *value)
Definition: ThreadLocal.h:38
~ThreadLocal()
Definition: ThreadLocal.h:92
static pthread_mutex_t keyLock
Definition: ThreadLocal.h:35
static std::map< pthread_key_t, std::set< void * > > keyInstances
Definition: ThreadLocal.h:34
ThreadLocal()
Create a new ThreadLocal variable.
Definition: ThreadLocal.h:81