thread.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2008 Marc Boris Duerner
3  * Copyright (C) 2006-2008 Tommi Maekitalo
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * As a special exception, you may use this file as part of a free
11  * software library without restriction. Specifically, if other files
12  * instantiate templates or use macros or inline functions from this
13  * file, or you compile this file and link it with other files to
14  * produce an executable, this file does not by itself cause the
15  * resulting executable to be covered by the GNU General Public
16  * License. This exception does not however invalidate any other
17  * reasons why the executable file might be covered by the GNU Library
18  * General Public License.
19  *
20  * This library is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23  * Lesser General Public License for more details.
24  *
25  * You should have received a copy of the GNU Lesser General Public
26  * License along with this library; if not, write to the Free Software
27  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28  */
29 
30 #ifndef CXXTOOLS_THREAD_H
31 #define CXXTOOLS_THREAD_H
32 
33 #include <cxxtools/noncopyable.h>
34 #include <cxxtools/callable.h>
35 #include <cxxtools/function.h>
36 #include <cxxtools/method.h>
37 #include <cxxtools/timespan.h>
38 
39 namespace cxxtools
40 {
41 
63  class Thread : protected NonCopyable
64  {
65  public:
67  enum State
68  {
69  Ready = 0,
70  Running = 1,
71  Finished = 2
72  };
73 
74  protected:
81  Thread();
82 
89  explicit Thread(const Callable<void>& cb);
90 
96  void init(const Callable<void>& cb);
97 
98  public:
104  virtual ~Thread();
105 
107  State state() const
108  { return _state; }
109 
115  void start();
116 
117  void create()
118  { this->start(); }
119 
126  static void exit();
127 
134  static void yield();
135 
152  static void sleep(const Milliseconds& ts);
153 
154  protected:
156  void detach();
157 
159  void join();
160 
162  bool joinNoThrow();
163 
165  void terminate();
166 
167  private:
169  Thread::State _state;
170 
172  class ThreadImpl* _impl;
173  };
174 
211  class AttachedThread : public Thread
212  {
213  public:
220  explicit AttachedThread(const Callable<void>& cb)
221  : Thread(cb)
222  {}
223 
226  {
228  }
229 
235  void join()
236  {
237  Thread::join();
238  }
239 
244  void terminate()
245  {
247  }
248  };
249 
282  class DetachedThread : public Thread
283  {
284  typedef void (*FuncPtrT)();
285 
286  public:
287  explicit DetachedThread(FuncPtrT fp)
288  : Thread( callable(fp) )
289  {
290  Thread::detach();
291  }
292 
293  protected:
303  : Thread()
304  {
305  Thread::init( callable(*this, &DetachedThread::exec) );
306  Thread::detach();
307  }
308 
314  virtual void destroy()
315  { delete this; }
316 
322  virtual void run()
323  {}
324 
325  private:
327  void exec()
328  {
329  this->run();
330  this->destroy();
331  }
332  };
333 
334 } // !namespace cxxtools
335 
336 #endif // namespace cxxtools