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