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 #if !defined(CXXTOOLS_SYSTEM_THREAD_H)
30 #define CXXTOOLS_SYSTEM_THREAD_H
31 
32 #include <cxxtools/noncopyable.h>
33 #include <cxxtools/callable.h>
34 #include <cxxtools/function.h>
35 #include <cxxtools/method.h>
36 
37 namespace cxxtools {
38 
60  class Thread : protected NonCopyable
61  {
62  public:
64  enum State
65  {
66  Ready = 0,
67  Running = 1,
68  Finished = 2
69  };
70 
71  protected:
78  Thread();
79 
86  explicit Thread(const Callable<void>& cb);
87 
93  void init(const Callable<void>& cb);
94 
95  public:
101  virtual ~Thread();
102 
104  State state() const
105  { return _state; }
106 
112  void start();
113 
114  void create()
115  { this->start(); }
116 
123  static void exit();
124 
131  static void yield();
132 
138  static void sleep(unsigned int ms);
139 
140  protected:
142  void detach();
143 
145  void join();
146 
148  bool joinNoThrow();
149 
151  void terminate();
152 
153  private:
155  Thread::State _state;
156 
158  class ThreadImpl* _impl;
159  };
160 
197  class AttachedThread : public Thread
198  {
199  public:
206  explicit AttachedThread(const Callable<void>& cb)
207  : Thread(cb)
208  {}
209 
212  {
214  }
215 
221  void join()
222  {
223  Thread::join();
224  }
225 
230  void terminate()
231  {
233  }
234  };
235 
268  class DetachedThread : public Thread
269  {
270  typedef void (*FuncPtrT)();
271 
272  public:
273  explicit DetachedThread(FuncPtrT fp)
274  : Thread( callable(fp) )
275  {
276  Thread::detach();
277  }
278 
279  protected:
289  : Thread()
290  {
291  Thread::init( callable(*this, &DetachedThread::exec) );
292  Thread::detach();
293  }
294 
300  virtual void destroy()
301  { delete this; }
302 
308  virtual void run()
309  {}
310 
311  private:
313  void exec()
314  {
315  this->run();
316  this->destroy();
317  }
318  };
319 
320 } // !namespace cxxtools
321 
322 #endif // namespace cxxtools