FX::QThreadLocalStorage< type > Class Template Reference

#include <QThread.h>

Inherits FX::QThreadLocalStorageBase.

List of all members.


Detailed Description

template<class type>
class FX::QThreadLocalStorage< type >

Permits process-global storage local to a thread only.

QThreadLocalStorage represents data which is local to a thread only ie; when queryed, the data returned is the same across any code in the process but changes according to which thread is currently executing. Multithreading novices may not be able to think of why you would ever want to use this (given that using the stack is usually sufficient), but experts will know there are some problems unsolvable without TLS (Thread Local Storage) nor ability to retrieve a unique thread id.

Tornado implements TLS using QThreadLocalStorageBase which basically permits setting and retrieving of a void *. QThreadLocalStorage is then implemented as a template over QThreadLocalStorageBase to make it more C++ friendly. To use QThreadLocalStorage, pass the pointer to the type of the data you wish to make thread-local:

class MyLocalData
{
    int foo;
};
static QThreadLocalStorage<MyLocalDataBase *> thrlocaldata;
...
// NOTE: Very very important you ALWAYS initialise on first use with TLS (think about it!)
if(!thrlocaldata) thrlocaldata=new MyLocalData;
...
if(thrlocaldata->foo>8) ...
Generally, though not always, you will use TLS as a static mostly-global variable.
Note:
Ensure you do not forget to release any memory stored into TLS. Have your thread's cleanup routine perform the appropriate checks or else use QThread::addCleanupCall().

Public Member Functions

 QThreadLocalStorage (type *initval=0)
type * operator= (type *a)
 operator type * () const
type * operator-> () const

Constructor & Destructor Documentation

template<class type>
FX::QThreadLocalStorage< type >::QThreadLocalStorage ( type *  initval = 0  )  [inline]

Constructs the TLS, optionally storing an initial value (for the current thread) immediately.


Member Function Documentation

template<class type>
type* FX::QThreadLocalStorage< type >::operator= ( type *  a  )  [inline]

Operator overload allowing the pointer to the type represented by this object to be set

template<class type>
FX::QThreadLocalStorage< type >::operator type * (  )  const [inline]

Operator overload permitting automatic retrieval of the pointer to the type represented by this object


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

(C) 2002-2008 Niall Douglas. Some parts (C) to assorted authors.
Generated on Fri Jun 13 22:28:55 2008 for TnFOX by doxygen v1.5.6