CPN
Computational Process Networks
PthreadKey.h
Go to the documentation of this file.
1 //=============================================================================
2 // PthreadKey template class.
3 //-----------------------------------------------------------------------------
4 // POSIX Pthread class library
5 // Copyright (C) 1997-1999 The University of Texas
6 //
7 // This library is free software; you can redistribute it and/or modify it
8 // under the terms of the GNU Library General Public License as published
9 // by the Free Software Foundation; either version 2 of the License, or
10 // (at your option) any later version.
11 //
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // Library General Public License for more details.
16 //
17 // The GNU Public License is available in the file LICENSE, or you
18 // can write to the Free Software Foundation, Inc., 59 Temple Place -
19 // Suite 330, Boston, MA 02111-1307, USA, or you can find it on the
20 // World Wide Web at http://www.fsf.org.
21 //=============================================================================
22 
23 #ifndef PthreadKey_h
24 #define PthreadKey_h
25 #pragma once
26 
27 #include <cpn/common.h>
29 #ifdef _POSIX_THREADS
30 
32 
33 
34 template<class T>
36  public:
37  PthreadKey(void (*destructor)(T) = 0)
38  { TrapError(pthread_key_create(&theKey, (void(*)(void*))destructor)); }
40  { try { TrapError(pthread_key_delete(theKey)); } catch (...) { std::terminate(); } }
41 
42  operator pthread_key_t* (void) { return &theKey; }
43  operator const pthread_key_t* (void) const { return &theKey; }
44 
45  T Get(void) const
46  { return (T)pthread_getspecific(theKey); }
47 
49  { TrapError(pthread_setspecific(theKey, (void*)val));
50  return *this;
51  }
52 
53  private:
54  pthread_key_t theKey;
55 };
56 
57 
58 #endif
59 #endif
PthreadKey< T > & Set(T val)
Definition: PthreadKey.h:48
void TrapError(int result)
PthreadKey(void(*destructor)(T)=0)
Definition: PthreadKey.h:37
pthread_key_t theKey
Definition: PthreadKey.h:54
~PthreadKey(void)
Definition: PthreadKey.h:39
T Get(void) const
Definition: PthreadKey.h:45