par::sys::specific< T > Struct Template Reference

Provide thread specific global variables. More...

#include <par_sys_specific.h>

Inheritance diagram for par::sys::specific< T >:

Inheritance graph
[legend]
Collaboration diagram for par::sys::specific< T >:

Collaboration graph
[legend]

List of all members.

Public Member Functions

T & cast ()
void dynamic_init (void)
 operator T & (void)
 ~specific (void)

Static Public Member Functions

static void static_destruction (void *t)
 Destroy the part that is common to all threads.
static void static_init (void *t, void const *)
 Initialize the part that is common to all threads.
static void static_init (void)

Public Attributes

pthread_key_t key
once_t trigger


Detailed Description

template<class T>
struct par::sys::specific< T >

Provide thread specific global variables.

This class implements transparent acces to the posix tools arround pthread_getspecific and enable you the view of a per thread variable with static storage class of type T.

A variable of type specific<T> should never be of storage class automatic. First of all this makes not much sense, usual variables on the stack fulfill this role perfectly. Then this would very much likely crash your program...

Use such a variable in your code (almost) as you would use a variable of storage class ``static'', so like a variable that you declared ``static'' or in the global scope of a program file. You can read it or assign to it with no problem.

The ``almost'' is for some cases where C++ due to its expressional incapacities pretends not to know what you'd like to do with the variable. In particular this happens when you pass such a variable to a function with a ``...'' parameter list, such as par_report. Here you have to cast your variable to T. Since this may turn your code unreadable we provide methods named ``cast()'' for that purpose.

Precondition:
T must implement
  • a constructor from void
  • a destructor
  • an assignment ``T const&''. All three must be accessible by par::sys::specific<T>. So if they are not public you should declare specific<T> a friend of T.
The total number of variables of type specific<T> is bound by the value of PTHREAD_KEYS_MAX. So since this is a limited resource, don't abuse it.

All of this only makes sense for if the variable is of static linkage. A specific variable has no constructor of its own so you have to initialize it statically. Use the macro PAR_SYS_SPECIFIC_CONST() for it as in

  static
  par::sys::once< double > specificVal  =
    PAR_SYS_SPECIFIC_CONST(specificVal);

Definition at line 78 of file par_sys_specific.h.


Constructor & Destructor Documentation

template<class T>
par::sys::specific< T >::~specific ( void   )  [inline]

The destructor of a specific variable is quite particular.

Since we assume that this is of static linkage it should be called exactly once by the very last remaining thread. All other per-thread copies of the variables are freed by calls to destroy(void*) when the individual threads terminate but the one of this thread here must be destroyed before the destructor for key_t is called.

Definition at line 174 of file par_sys_specific.h.


Member Function Documentation

template<class T>
T& par::sys::specific< T >::cast ( void   )  [inline]

Definition at line 163 of file par_sys_specific.h.

template<class T>
void par::sys::specific< T >::dynamic_init ( void   )  [inline]

Definition at line 120 of file par_sys_specific.h.

template<class T>
par::sys::specific< T >::operator T & ( void   )  [inline]

Definition at line 159 of file par_sys_specific.h.

template<class T>
static void par::sys::specific< T >::static_destruction ( void *  t  )  [inline, static]

Destroy the part that is common to all threads.

Definition at line 96 of file par_sys_specific.h.

template<class T>
static void par::sys::specific< T >::static_init ( void *  t,
void const *   
) [inline, static]

Initialize the part that is common to all threads.

Definition at line 88 of file par_sys_specific.h.

template<class T>
static void par::sys::specific< T >::static_init ( void   )  [static]


Member Data Documentation

template<class T>
pthread_key_t par::sys::specific< T >::key

Definition at line 84 of file par_sys_specific.h.

template<class T>
once_t par::sys::specific< T >::trigger

Definition at line 81 of file par_sys_specific.h.


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

Generated on Tue Oct 13 22:04:31 2009 for parXXL by  doxygen 1.5.8