Thread.h
1 /*
2  * Copyright (C) 2006-2013 Marc Boris Duerner
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 
29 #ifndef PT_SYSTEM_THREAD_H
30 #define PT_SYSTEM_THREAD_H
31 
32 #include <Pt/System/Api.h>
33 #include <Pt/NonCopyable.h>
34 #include <Pt/Callable.h>
35 #include <Pt/Function.h>
36 #include <Pt/Method.h>
37 
38 namespace Pt {
39 
40 namespace System {
41 
42 class EventLoop;
43 
64 class PT_SYSTEM_API Thread : protected NonCopyable
65 {
66  public:
68  enum State
69  {
70  Ready = 0,
71  Running = 1,
72  Joined = 2,
73  Detached = 3
74  };
75 
76  public:
83  Thread();
84 
91  explicit Thread(const Callable<void>& cb);
92 
99  explicit Thread(EventLoop& loop);
100 
106  void init(const Callable<void>& cb);
107 
113  virtual ~Thread();
114 
116  State state() const
117  { return _state; }
118 
119  bool isJoinable() const
120  { return _state == Running; }
121 
127  void start();
128 
130  void detach();
131 
133  void join();
134 
141  static void exit();
142 
149  static void yield();
150 
156  static void sleep(unsigned int ms);
157 
158  protected:
160  bool joinNoThrow();
161 
162  private:
164  Thread::State _state;
165 
167  bool _detach;
168 
170  class ThreadImpl* _impl;
171 };
172 
208 class AttachedThread : public Thread
209 {
210  public:
217  : Thread()
218  {}
219 
226  explicit AttachedThread(const Callable<void>& cb)
227  : Thread(cb)
228  {}
229 
236  explicit AttachedThread(EventLoop& loop)
237  : Thread(loop)
238  {}
239 
242  {
243  Thread::joinNoThrow();
244  }
245 };
246 
247 
281 class DetachedThread : public Thread
282 {
283  typedef void (*FuncPtrT)();
284 
285  public:
286  explicit DetachedThread(FuncPtrT fp)
287  : Thread( callable(fp) )
288  {
289  Thread::detach();
290  }
291 
292  protected:
302  : Thread()
303  {
304  Thread::init( callable(*this, &DetachedThread::exec) );
305  Thread::detach();
306  }
307 
313  virtual void destroy()
314  { delete this; }
315 
321  virtual void run()
322  {}
323 
324  private:
326  void exec()
327  {
328  this->run();
329  this->destroy();
330  }
331 };
332 
333 } // namespace System
334 
335 } // namespace Pt
336 
337 #endif // PT_SYSTEM_THREAD_H
State state() const
Returns the current state of the thread.
Definition: Thread.h:116
Protects derived classes from being copied.
Definition: NonCopyable.h:54
AttachedThread()
Default Constructor.
Definition: Thread.h:216
A Joinable thread.
Definition: Thread.h:208
State
Status of a thread.
Definition: Thread.h:68
AttachedThread(const Callable< void > &cb)
Constructs a thread with a thread entry.
Definition: Thread.h:226
A detached thread.
Definition: Thread.h:281
Thread()
Default Constructor.
void init(const Callable< void > &cb)
Initialize with a thread entry.
An interface for all callable entities.
Definition: Callable.h:13
DetachedThread()
Constructs a detached thread.
Definition: Thread.h:301
~AttachedThread()
Joins the thread, if not already joined.
Definition: Thread.h:241
Platform independent threads.
Definition: Thread.h:64
virtual void destroy()
Destroys a detached thread.
Definition: Thread.h:313
Thread-safe event loop supporting I/O multiplexing and Timers.
Definition: EventLoop.h:72
virtual void run()
Thread entry method.
Definition: Thread.h:321
void detach()
Detaches the thread.
AttachedThread(EventLoop &loop)
Constructs a thread with an event loop.
Definition: Thread.h:236