When you create a thread local storage object, it automatically sets up per-thread storage for you. It lazily creates storage as required so its efficient. Since it is a template class, you must specialize it for a particular class type, which makes it typesafe as well. Once an object is created, each thread can treat it as though it were the only user of the object.
In effect, IThreadLocalStorage is like a smart pointer except that it provides a unique pointer for each thread.
Constructors & DestructorConstruct and destruct IThreadLocalStorage objects.
![]() |
public:
~IThreadLocalStorage()
| Windows | OS/2 | AIX |
| Yes | Yes | Yes |
![]() |
public:
IThreadLocalStorage(const bool resettable)
| Windows | OS/2 | AIX |
| Yes | Yes | Yes |
public:
IThreadLocalStorage()
| Windows | OS/2 | AIX |
| Yes | Yes | Yes |
Access OperatorsThese operators allow you to access the stored data pointer. These only allow the calling thread to access the copy of the object that belongs to it!
![]() |
public:
AType& operator *() const
| Windows | OS/2 | AIX |
| Yes | Yes | Yes |
![]() |
public:
AType* operator ->() const
| Windows | OS/2 | AIX |
| Yes | Yes | Yes |
Storage Management MethodsThese methods allow you to manage the object stored in the thread local storage object. These methods only work on the copy of the object that belongs to the calling thread!
![]() |
public:
void adoptStorage(AType* toadopt)
If a thread never accesses a thread local storage object, then no data will ever be created for that thread. This is an efficient and simple approach, but it does mean that you must always check for the existence of the data (and allocate it if not) before accessing it.
Note that each thread local storage object can be set up to allow or disallow multiple sets of the data object. If the object allows it, then calling adoptStorage() again will cause the previous object to be deleted and the new object stored. If the object does not allow it, then an InvalidRequest exception is thrown.
| IInvalidRequest | is thrown if the calling thread has already stored an object, and this object does not allow resetting of the data value. The ability to reset is indicated during construction. |
| Windows | OS/2 | AIX |
| Yes | Yes | Yes |
![]() |
public:
void freeStorage()
| Windows | OS/2 | AIX |
| Yes | Yes | Yes |
![]() |
public:
AType* get() const
| Windows | OS/2 | AIX |
| Yes | Yes | Yes |
![]() |
public:
AType* storage() const
| Windows | OS/2 | AIX |
| Yes | Yes | Yes |
static void freeAllStorage()
virtual ~IThreadLocalBase()
void expandThreadSlots(const unsigned long newcount)
virtual void freeStorage() = 0
unsigned long getNextSlot()
void* getSlot(const unsigned long index) const
IThreadLocalBase()
IThreadLocalBase(const IThreadLocalBase&)
IThreadLocalBase& operator =(const IThreadLocalBase)
void setSlot(const unsigned long index, void* data)