]> git.tdb.fi Git - libs/core.git/blob - source/core/thread.h
ae257078569c47844e0d4502886f5de403371885
[libs/core.git] / source / core / thread.h
1 #ifndef MSP_CORE_THREAD_H_
2 #define MSP_CORE_THREAD_H_
3
4 namespace Msp {
5
6 /**
7 Base class for threads.  To create a thread for some task, derive it from this
8 class and implement the main() function.  Note that threads are not
9 automatically started upon creation - you must manually call launch() instead.
10 This is to allow initializing variables of the derived class before the thread
11 is started.
12 */
13 class Thread
14 {
15 private:
16         struct Private;
17
18         Private *priv_;
19         bool launched_;
20
21 protected:
22         Thread();
23 private:
24         Thread(const Thread &);
25         Thread &operator=(const Thread &);
26 public:
27         virtual ~Thread();
28
29         /** Waits for the thread to exit.  Calling this from the thread will cause a
30         deadlock. */
31         void join();
32
33         /** Violently terminates the thread.  This should only be used as a last
34         resort, as the thread gets no chance to clean up. */
35         void kill();
36 protected:
37         void launch();
38         virtual void main() = 0;
39 };
40
41 } // namespace Msp
42
43 #endif