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
38namespace Pt {
39
40namespace System {
41
42class EventLoop;
43
81class PT_SYSTEM_API Thread : protected NonCopyable
82{
83 public:
85 enum State
86 {
87 Ready = 0,
88 Running = 1,
89 Joined = 2,
91 };
92
93 public:
101
108 explicit Thread(const Callable<void>& cb);
109
116 explicit Thread(EventLoop& loop);
117
123 void init(const Callable<void>& cb);
124
130 virtual ~Thread();
131
133 State state() const
134 { return _state; }
135
136 bool isJoinable() const
137 { return _state == Running; }
138
144 void start();
145
147 void detach();
148
150 void join();
151
158 static void exit();
159
166 static void yield();
167
173 static void sleep(unsigned int ms);
174
175 protected:
177 bool joinNoThrow();
178
179 private:
181 Thread::State _state;
182
184 bool _detach;
185
187 class ThreadImpl* _impl;
188};
189
198class AttachedThread : public Thread
199{
200 public:
207 : Thread()
208 {}
209
216 explicit AttachedThread(const Callable<void>& cb)
217 : Thread(cb)
218 {}
219
226 explicit AttachedThread(EventLoop& loop)
227 : Thread(loop)
228 {}
229
232 {
233 Thread::joinNoThrow();
234 }
235};
236
237
247class DetachedThread : public Thread
248{
249 typedef void (*FuncPtrT)();
250
251 public:
252 explicit DetachedThread(FuncPtrT fp)
253 : Thread( callable(fp) )
254 {
256 }
257
258 protected:
268 : Thread()
269 {
270 Thread::init( callable(*this, &DetachedThread::exec) );
272 }
273
275 { }
276
282 virtual void destroy()
283 { delete this; }
284
290 virtual void run()
291 {}
292
293 private:
295 void exec()
296 {
297 this->run();
298 this->destroy();
299 }
300};
301
302} // namespace System
303
304} // namespace Pt
305
306#endif // PT_SYSTEM_THREAD_H
An interface for all callable entities.
Definition Callable.h:48
ConstMethod< R, ClassT, As... > callable(ClassT &object, R(BaseT::*method)(As...) const)
Returns a ConstMethod object for the given object/method pair.
Definition ConstMethod.h:161
NonCopyable()
Default constructor.
Definition NonCopyable.h:63
AttachedThread(EventLoop &loop)
Constructs a thread with an event loop.
Definition Thread.h:226
~AttachedThread()
Joins the thread, if not already joined.
Definition Thread.h:231
AttachedThread()
Default Constructor.
Definition Thread.h:206
AttachedThread(const Callable< void > &cb)
Constructs a thread with a thread entry.
Definition Thread.h:216
Thread that runs without a waiter.
Definition Thread.h:248
DetachedThread()
Constructs a detached thread.
Definition Thread.h:267
virtual void run()
Thread entry method.
Definition Thread.h:290
virtual void destroy()
Destroys a detached thread.
Definition Thread.h:282
Event loop of a thread or process.
Definition EventLoop.h:83
Thread(EventLoop &loop)
Constructs a thread with an event loop.
virtual ~Thread()
Destructor.
static void sleep(unsigned int ms)
Sleep for some time.
State
Status of a thread.
Definition Thread.h:86
@ Ready
Not started yet.
Definition Thread.h:87
@ Running
Thread is running.
Definition Thread.h:88
@ Joined
Joined with parent thread.
Definition Thread.h:89
@ Detached
Detached from parent thread.
Definition Thread.h:90
void start()
Starts the thread.
void join()
Wait for the thread to finish execution.
Thread()
Default Constructor.
Thread(const Callable< void > &cb)
Constructs a thread with a thread entry.
void init(const Callable< void > &cb)
Initialize with a thread entry.
static void exit()
Exits athread.
void detach()
Detaches the thread.
State state() const
Returns the current state of the thread.
Definition Thread.h:133
static void yield()
Yield CPU time.
System programming
Definition Connection.h:26
Core module.
Definition Allocator.h:33