ClanLib  2.3.7
thread.h
Go to the documentation of this file.
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2011 The ClanLib Team
4 **
5 ** This software is provided 'as-is', without any express or implied
6 ** warranty. In no event will the authors be held liable for any damages
7 ** arising from the use of this software.
8 **
9 ** Permission is granted to anyone to use this software for any purpose,
10 ** including commercial applications, and to alter it and redistribute it
11 ** freely, subject to the following restrictions:
12 **
13 ** 1. The origin of this software must not be misrepresented; you must not
14 ** claim that you wrote the original software. If you use this software
15 ** in a product, an acknowledgment in the product documentation would be
16 ** appreciated but is not required.
17 ** 2. Altered source versions must be plainly marked as such, and must not be
18 ** misrepresented as being the original software.
19 ** 3. This notice may not be removed or altered from any source distribution.
20 **
21 ** Note: Some of the libraries ClanLib may link to may have additional
22 ** requirements or restrictions.
23 **
24 ** File Author(s):
25 **
26 ** Magnus Norddahl
27 */
28 
31 
32 #pragma once
33 
34 
35 #include "../api_core.h"
36 #include "sharedptr.h"
37 #include "runnable.h"
38 #include "exception.h"
39 
40 class CL_Thread_Impl;
41 
46 {
49 
50 public:
52  CL_Thread();
53 
54  ~CL_Thread();
55 
59 
60 public:
61 
65 
66 public:
68  void start(CL_Runnable *runnable);
69 
70  template<class C>
71  void start(C *instance, void (C::*member)())
72  {
73  CL_Runnable *r = new CL_RunnableMember_v0<C>(instance, member);
74  try
75  {
76  start(r);
77  }
78  catch (const CL_Exception&)
79  {
80  delete r;
81  throw;
82  }
83  }
84 
85  template<class C, class P1>
86  void start(C *instance, void (C::*member)(P1 p1), P1 p1)
87  {
88  CL_Runnable *r = new CL_RunnableMember_v1<C, P1>(instance, member, p1);
89  try
90  {
91  start(r);
92  }
93  catch (const CL_Exception&)
94  {
95  delete r;
96  throw;
97  }
98  }
99 
100  template<class C, class P1, class P2>
101  void start(C *instance, void (C::*member)(P1 p1, P2 p2), P1 p1, P2 p2)
102  {
103  CL_Runnable *r = new CL_RunnableMember_v2<C, P1, P2>(instance, member, p1, p2);
104  try
105  {
106  start(r);
107  }
108  catch (const CL_Exception&)
109  {
110  delete r;
111  throw;
112  }
113  }
114 
115  template<class C, class P1, class P2, class P3>
116  void start(C *instance, void (C::*member)(P1 p1, P2 p2, P3 p3), P1 p1, P2 p2, P3 p3)
117  {
118  CL_Runnable *r = new CL_RunnableMember_v3<C, P1, P2, P3>(instance, member, p1, p2, p3);
119  try
120  {
121  start(r);
122  }
123  catch (const CL_Exception&)
124  {
125  delete r;
126  throw;
127  }
128  }
129 
130  template<class C, class P1, class P2, class P3, class P4>
131  void start(C *instance, void (C::*member)(P1 p1, P2 p2, P3 p3, P4 p4), P1 p1, P2 p2, P3 p3, P4 p4)
132  {
133  CL_Runnable *r = new CL_RunnableMember_v4<C, P1, P2, P3, P4>(instance, member, p1, p2, p3, p4);
134  try
135  {
136  start(r);
137  }
138  catch (const CL_Exception&)
139  {
140  delete r;
141  throw;
142  }
143  }
144 
145  template<class C, class P1, class P2, class P3, class P4, class P5>
146  void start(C *instance, void (C::*member)(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5), P1 p1, P2 p2, P3 p3, P4 p4, P5 p5)
147  {
148  CL_Runnable *r = new CL_RunnableMember_v5<C, P1, P2, P3, P4, P5>(instance, member, p1, p2, p3, p4, p5);
149  try
150  {
151  start(r);
152  }
153  catch (const CL_Exception&)
154  {
155  delete r;
156  throw;
157  }
158  }
159 
161  void join();
162 
169  void kill();
170 
172 
173  static void set_thread_name(const char *name);
174 
178 
179 private:
180  CL_SharedPtr<CL_Thread_Impl> impl;
182 };
183