CPN
Computational Process Networks
PthreadLib.h
Go to the documentation of this file.
1 //=============================================================================
2 // Pthread 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 Pthread_h
24 #define Pthread_h
25 #pragma once
26 
27 #ifdef EXTERNAL_TEMPLATES
28 # pragma interface
29 #endif
30 #include <cpn/common.h>
32 #ifdef _POSIX_THREADS
33 
37 
38 //=============================================================================
39 // WARNING: subclassing class Pthread can lead to race conditions.
40 // It is recommended that you instead use PthreadFunctional.
41 //-----------------------------------------------------------------------------
42 // This Pthread class was written in the style of the Java Thread class.
43 // Unfortunately, C++ has some behaviors that can cause problems when you
44 // use inheritance with a thread class. Some simple rules can mitigate:
45 // 1) NEVER call Start() in the constructor of a derived class. There are
46 // no virtual methods inside of C++ constructors, and Pthread::EntryPoint()
47 // must be virtual. This can lead to attempted execution before the
48 // vtables are initialized.
49 // 2) ALWAYS call Join() before doing anything else in the destructor of EVERY
50 // class that is an inheritance descendent of the Pthread class. Because of
51 // the calling order of destructors, your destructor could be executing at
52 // the same time as your spawned thread. This could lead to your thread
53 // operating on descructed data and deallocated memory.
54 //=============================================================================
55 
56 class Pthread : public PthreadBase {
57  public:
58  Pthread(void);
59  Pthread(const PthreadAttr& attr);
60  virtual ~Pthread(void);
61 
62  void Start(void);
63 
64  int Running(void) {
66  return state == running;
67  }
68 
72  int Done(void) {
74  return state == done || state == joined;
75  }
76 
77  void *Join(void);
78 
79  protected:
80  virtual void* EntryPoint(void) = 0;
81 // virtual void Cleanup(void) { }
82 
83  private:
86  void *returnResult;
87  bool inJoin;
88 
91 
92  static void* PthreadEntryPoint(void* arg);
93  static void PthreadCleanup(void* arg);
94 };
95 
96 
97 #endif
98 #endif
static void * PthreadEntryPoint(void *arg)
Definition: PthreadLib.cc:131
Pthread(void)
Definition: PthreadLib.cc:32
int Running(void)
Definition: PthreadLib.h:64
static void PthreadCleanup(void *arg)
Definition: PthreadLib.cc:154
int Done(void)
Definition: PthreadLib.h:72
PthreadCondition cond
Definition: PthreadLib.h:85
virtual ~Pthread(void)
Definition: PthreadLib.cc:67
PthreadState state
Definition: PthreadLib.h:90
PthreadMutex mutex
Definition: PthreadLib.h:84
void * Join(void)
Definition: PthreadLib.cc:108
void * returnResult
Definition: PthreadLib.h:86
PthreadState
Definition: PthreadLib.h:89
bool inJoin
Definition: PthreadLib.h:87
virtual void * EntryPoint(void)=0
void Start(void)
Definition: PthreadLib.cc:100