libcommonc++  0.7
Thread.h++
Go to the documentation of this file.
1 /* ---------------------------------------------------------------------------
2  commonc++ - A C++ Common Class Library
3  Copyright (C) 2005-2014 Mark A Lindner
4 
5  This file is part of commonc++.
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (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  You should have received a copy of the GNU Library General Public
18  License along with this library; if not, write to the Free
19  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  ---------------------------------------------------------------------------
21 */
22 
23 #ifndef __ccxx_Thread_hxx
24 #define __ccxx_Thread_hxx
25 
26 #include <commonc++/Common.h++>
28 #include <commonc++/Runnable.h++>
30 #include <commonc++/String.h++>
31 
32 #ifdef CCXX_OS_POSIX
33 #include <pthread.h>
34 #endif
35 
36 namespace ccxx {
37 
56 {
57  public:
58 
67  Thread(bool detached = false, size_t stackSize = 0);
68 
79  Thread(Runnable* runnable, bool detached = false, size_t stackSize = 0);
80 
82  virtual ~Thread();
83 
90  virtual void start();
91 
97  virtual void stop();
98 
106  bool join();
107 
109  bool isRunning() const;
110 
112  inline bool isDetached() const
113  { return(_detached); }
114 
116  void setPriority(Priority priority);
117 
119  Priority getPriority() const;
120 
122  inline void setName(const String& name)
123  { _name = name; }
124 
126  inline String getName() const
127  { return(_name); }
128 
134  static void sleep(timespan_ms_t msec);
135 
142  static Thread* currentThread();
143 
145  static ThreadID currentThreadID();
146 
147  protected:
148 
153  virtual void run();
154 
160  virtual void cleanup();
161 
170  bool trySleep(timespan_ms_t msec);
171 
178  bool testCancel();
179 
181  void yield();
182 
184  static void exit();
185 
186  private:
187 
188  Thread(ThreadHandle id);
189 
190  AtomicCounter _runFlag;
191  bool _cancelled;
192  bool _detached;
193  size_t _stackSize;
194  String _name;
195 #ifdef CCXX_OS_WINDOWS
196  HANDLE _thread;
197  HANDLE _cancelEvent;
198  bool _cleanupDone;
199 #else
200  pthread_t _thread;
201  pthread_attr_t _attrs;
202 #endif
203  Runnable* _runnable;
204 
205 #ifdef CCXX_OS_WINDOWS
206  static DWORD WINAPI _startupDispatcher(LPVOID arg);
207  static void _cleanupTLS();
208 #else
209  static void* _startupDispatcher(void* arg);
210  static void _cleanupDispatcher(void* arg);
211 #endif
212 
213  void _run();
214  void _init();
215 
217 };
218 
219 } // namespace ccxx
220 
221 #endif // __ccxx_Thread_hxx
void setName(const String &name)
Set the name of this thread.
Definition: Thread.h++:122
String getName() const
Get the name of this thread.
Definition: Thread.h++:126
pthread_t ThreadHandle
Definition: Common.h++:223
pthread_t ThreadID
Definition: Common.h++:224
An integer counter whose value is modified in an atomic fashion.
Definition: AtomicCounter.h++:36
#define COMMONCPP_API
Definition: Common.h++:126
A flexible, reference counted, copy-on-write, thread-safe, nullable string.
Definition: String.h++:50
Priority
Thread and Process priority levels.
Definition: Common.h++:307
#define CCXX_COPY_DECLS(CLASS)
Inlines declarations of a copy constructor and assignment operator for the class CLASS.
Definition: Common.h++:295
A runnable object.
Definition: Runnable.h++:35
int timespan_ms_t
A timespan expressed in milliseconds.
Definition: Integers.h++:104
A thread of execution.
Definition: Thread.h++:55
Definition: AllocationMap.c++:25
bool isDetached() const
Test if the thread is detached.
Definition: Thread.h++:112