]> git.tdb.fi Git - libs/core.git/blob - source/core/thread.h
Make sure all files have the correct header
[libs/core.git] / source / core / thread.h
1 /* $Id$
2
3 This file is part of libmspcore
4 Copyright © 2006 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #ifndef MSP_CORE_THREAD_H_
9 #define MSP_CORE_THREAD_H_
10
11 #include "types.h"
12
13 namespace Msp {
14
15 /**
16 Base class for threads.  To create a thread for some task, derive it from this
17 class and implement the main() function.  Note that threads are not
18 automatically started upon creation - you must manually call launch() instead.
19 This is to allow initializing variables of the derived class before the thread
20 is started.
21 */
22 class Thread
23 {
24 private:
25         ThreadHandle thread_;
26         bool         launched_;
27
28 protected:
29         Thread(): launched_(false) { }
30 public:
31         virtual ~Thread();
32
33         void join();
34         void cancel();
35         void kill();
36 protected:
37         void launch();
38         virtual void main()=0;
39         void check_cancel();
40
41 private:
42         static
43 #ifdef WIN32
44         DWORD WINAPI
45 #else
46         void *
47 #endif
48         main_(void *t) { (reinterpret_cast<Thread *>(t))->main(); return 0; }
49
50         Thread(const Thread &);
51         Thread &operator=(const Thread &);
52 };
53
54 } // namespace Msp
55
56 #endif