Main Page   Namespace List   Class Hierarchy   Compound List   Namespace Members   Compound Members  

ThreadLocal Class Template Reference

#include <ThreadLocal.h>

Inheritance diagram for ThreadLocal:

AbstractThreadLocal NonCopyable InheritableThreadLocal List of all members.

Public Methods

virtual ~ThreadLocal () throw ()
 Destroy this ThreadLocal.

get () const throw ()
set (T val) const throw ()

Protected Methods

virtual void * initialValue () const throw ()

Detailed Description

template<class T>
class ZThread::ThreadLocal< T >

Author:
Eric Crahen <crahen@cse.buffalo.edu>
Date:
<2002-05-30T17:50:36-0400>
Version:
2.2.0
Provides a method to access the local storage of each thread. No matter what thread access this object, it will always store values unique to each thread. This is done using the local storage allocated by the OS for each thread. It doesn't require any locking and is very fast

The first time a ThreadLocal veriable is accessed by a thread the initialValue() method will be invoked. This allows subclasses to perfrom any special actions they might need to when a new thread uses one of these variables.

The destroyValue() method is invoked when a thread that has used a ThreadLocal is about to exit.

The recommended usage of this object would be something like in the example shown below

 class MyClass {
 protected:

 static ThreadLocal<int> _threadKey;

 public:
 
 int getValue() const {
   return _threadKey.get();
 }
 
 int setValue(int n) const {
   return _threadKey.set(n);
 }

 };

The ThreadLocal object itself acts as the key, instead of some arbitrary integer that needed to be defined by the programmer before. This is a much more elegant solution.

See also:
AbstractThreadLocal


Member Function Documentation

T get   const throw () [inline]
 

Get the value associated with the current thread and this object via fetch(). If no association exists, then initialValue() is invoked.

Returns :
T associated value
Exceptions:
Synchronization_Exception  - thrown if there is an error allocating native thread local storage

Reimplemented from AbstractThreadLocal.

virtual void* initialValue   const throw () [inline, protected, virtual]
 

Invoked by the framework the first time a get() or set() is invoked on a ThreadLocal variable from a particular thread.

Returns :
void* - value that will be set for the executing thread

Implements AbstractThreadLocal.

T set   val const throw () [inline]
 

Set the value associated with the current thread and this object. This value can only be retrieved from the current thread.

Parameters:
T  new associated value
Returns :
T old associated value, if no association exists, then initialValue() is invoked.
Exceptions:
Synchronization_Exception  - thrown if there is an error allocating native thread local storage


The documentation for this class was generated from the following file:
Generated on Tue Aug 27 07:43:15 2002 for ZThread by doxygen1.2.17