FX::QThreadLocalStorage< type > Class Template Reference

#include <QThread.h>

Inheritance diagram for FX::QThreadLocalStorage< type >:

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

Definition at line 836 of file QThread.h.

Public Member Functions

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


The documentation for this class was generated from the following file:
(C) 2002-2009 Niall Douglas. Some parts (C) to assorted authors.
Generated on Fri Nov 20 18:37:46 2009 for TnFOX by doxygen v1.4.7