Thread Local Storage (TLS) class. More...
#include <Inventor/threads/SbThreadLocalStorage.h>
Classes | |
class | StorageInfo |
Static Public Member Functions | |
static size_t | createStorage (const size_t byteSize, SoInitTLSClassCB *initFunc=NULL, SoExitTLSClassCB *exitFunc=NULL, const char *funcName=NULL) |
static void | deleteStorage (size_t id) |
static void * | getStorage (const size_t classThreadId) |
This class allows to create thread local data storage. This allow to manage per thread static global variables in multithread mode.
Example on how to use the Thread Local Storage (TLS):
In this example we move static variables s_myStaticVar1 and s_myStaticVar2 of the class MyClass to Thread Local Storage,
class myClass { static void initClass(); static void exitClass(); ... static int s_myStaticVar1; static float* s_myStaticVar2; ... } void myClass::initClass() { s_myStaticVar1 = 32; s_myStaticVar2 = new float[32]; } void myClass::exitClass() { s_myStaticVar1 = 0; delete [] s_myStaticVar2; }
class myClass { static void initClass(); static void exitClass(); // Declare necessary members and methods for TLS usage SB_THREAD_TLS_HEADER(); ... // structure to handle per thread static variables struct MTstruct { int s_myStaticVar1; float* s_myStaticVar2; } ... } // Properly initilize members and methods for TLS usage SB_THREAD_TLS_SOURCE( myClass ); void myClass::initClass() { // register and reserve space for TLS SB_THREAD_INIT_CLASS(myClass, MTstruct); } void myClass::exitClass() { } void myClass::initTLSClass() { // get access to TLS and init/allocate variables once per thread struct myClass::MTstruct * mtstruct = (struct myClass::MTstruct *)GET_THREAD_LOCAL_STORAGE(myClass) ; mtstruct->s_myStaticVar1 = 32; mtstruct->s_myStaticVar2 = new float[32]; } void myClass::exitTLSClass() { // get access to TLS and deallocate variables once per thread struct myClass::MTstruct * mtstruct = (struct myClass::MTstruct *)GET_THREAD_LOCAL_STORAGE(myClass) ; delete [] mtstruct->s_myStaticVar2; }
struct myClass::MTstruct * mtstruct = (struct myClass::MTstruct *) GET_THREAD_LOCAL_STORAGE(myClass) ;
GET_THREAD_LOCAL_VAR(myClass, MTstruct, variable1)
SbThread, SbThreadAutoLock, SbThreadAutoReadLock, SbThreadAutoWriteLock, SbThreadBarrier, SbThreadRWMutex, SbThreadSignal
static size_t SbThreadLocalStorage::createStorage | ( | const size_t | byteSize, | |
SoInitTLSClassCB * | initFunc = NULL , |
|||
SoExitTLSClassCB * | exitFunc = NULL , |
|||
const char * | funcName = NULL | |||
) | [static] |
Creates or updates the local storage for the current thread.
Requests that 'size' bytes of storage should be allocated for all threads for the calling class. Returns a unique Id to be used by getStorage method to access the storage.
static void SbThreadLocalStorage::deleteStorage | ( | size_t | id | ) | [static] |
Deletes the local storage for all threads.
static void* SbThreadLocalStorage::getStorage | ( | const size_t | classThreadId | ) | [static] |
Returns a pointer on the storage for the given Id.
This storage is guaranteed to be local to the current thread. The size of the storage is the same than what was requested by createStorage, unless the pointer returned is NULL.