]> git.tdb.fi Git - libs/core.git/blob - source/core/thread.h
Assimilate exceptions and RefPtr from mspmisc
[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 #ifndef MSP_CORE_THREAD_H_
8 #define MSP_CORE_THREAD_H_
9
10 #include "types.h"
11
12 namespace Msp {
13
14 /**
15 Base class for threads.  To create a thread for some task, derive it from this
16 class and implement the main() function.  Note that threads are not
17 automatically started upon creation - you must manually call launch() instead.
18 This is to allow initializing variables of the derived class before the thread
19 is started.
20 */
21 class Thread
22 {
23 public:
24         void join();
25         void cancel();
26         void kill();
27         virtual ~Thread();
28 protected:
29         Thread(): launched_(false) { }
30         void launch();
31         virtual void main()=0;
32         void check_cancel();
33 private:
34         ThreadHandle thread_;
35         bool         launched_;
36
37         Thread(const Thread &);
38         Thread &operator=(const Thread &);
39
40         static
41 #ifdef WIN32
42         DWORD WINAPI
43 #else
44         void *
45 #endif
46         main_(void *t) { ((Thread *)t)->main(); return 0; }
47 };
48
49 } // namespace Msp
50
51 #endif