Main Page Namespace List Class Hierarchy Compound List Compound Members

# ZThread::ThreadLocal Class Template Reference

`#include <ThreadLocal.h>`

Inheritance diagram for ZThread::ThreadLocal::

List of all members.

## Public Methods
| ThreadLocal () throw ()
virtual | ~ThreadLocal () throw ()
T | get () const throw (Synchronization_Exception)
T | set (T val) const throw (Synchronization_Exception)
## Protected Methods
virtual void* | initialValue (void *initValue) throw ()
virtual bool | propogateValue () throw ()

## Detailed Description

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

Author:
  Eric Crahen

Version:
  1.1.3

Date:
  01-02-2001

Provide 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 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.

## Constructor & Destructor Documentation

ThreadLocal ( | ) throw ()`[inline]`

| Create a new ThreadLocal object

~ThreadLocal ( | ) throw ()`[inline, virtual]`

| Destroy this ThreadLocal object

## Member Function Documentation

T get ( | ) const throw (Synchronization_Exception)`[inline]`

  | Get a value with this ThreadLocal object. Any value retrieved from this object will have been set from the same Thread. Exceptions:
  Synchronization_Exception | - thrown if there is an error allocating native thread local storage
Reimplemented from ZThread::AbstractThreadLocal.

void * initialValue ( | void * initValue ) throw ()`[inline, protected, virtual]`

  | Invoked by the framework the first time a ThreadLocal variable is used by a thread. Provides a location for subclasses to perform various per/thread initialization. Parameters:
  void | * initValue - the default value is always 0. Subclasses may overide this and return a new value or delegate up thier class hierarchy as they see fit.
  Returns: void* - value that will be set for the executing thread Parameters:
  initValue |
Reimplemented from ZThread::AbstractThreadLocal.

bool propogateValue ( | ) throw ()`[inline, protected, virtual]`

| Inform the framework this value does not propogate to child threads Returns: false - always Reimplemented from ZThread::AbstractThreadLocal. Reimplemented in ZThread::InheritableThreadLocal.

T set ( | T val ) const throw (Synchronization_Exception)`[inline]`

  | Set a value with this ThreadLocal object. This value can only be retrieved from this ThreadLocal object from the same Thread that set it. Exceptions:
  Synchronization_Exception | - thrown if there is an error allocating native thread local storage
  Parameters:
  val |

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

- ThreadLocal.h