File: threadfunc_v1.h

package info (click to toggle)
clanlib 0.5.4-1-6
  • links: PTS
  • area: main
  • in suites: woody
  • size: 10,320 kB
  • ctags: 10,893
  • sloc: cpp: 76,056; xml: 3,281; sh: 2,961; perl: 1,204; asm: 837; makefile: 775
file content (60 lines) | stat: -rw-r--r-- 1,346 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

#ifndef header_threadfunc_v1
#define header_threadfunc_v1

#include "thread.h"

template<class Class, class Param>
class CL_ThreadFunc_Runnable_v1 : public CL_Runnable
{
public:
	typedef void (Class::*MemberFunc)(Param &param);

	CL_ThreadFunc_Runnable_v1(Class *self, MemberFunc func, const Param &param)
	: self(self), func(func), param(param)
	{
	}

private:
	virtual void run()
	{
		(*self.*func)(param);
	}

	Class *self;
	MemberFunc func;
	Param param;
};

//: Member function based thread callback interface.
// The CL_ThreadFunc_v1<MyClass, MyParam> is an interface used to call a member function
// in a new thread.
//
// Following code demonstrates how it is used:
// <code>
// class MyClass
// {
//   CL_Thread thread;
//
//   MyClass() : thread(CL_ThreadFunc_v1(this, MyClass::worker_function, 15))
//   {
//     thread.start();
//   }
//
//   void worker_function(int value) { assert(value == 15); }
// };
// </code>
#define CL_ThreadFunc_v1(a, b, c) CL_Thread(new CL_ThreadFunc_Runnable_v1(a, b, c), true)

/* Following code procudes internal compiler error with msvc++
template<class Class, class Param>
CL_Thread CL_ThreadFunc_v1(
	Class *self,
	CL_ThreadFunc_Runnable_v1<Class, Param>::MemberFunc func,
	const Param &param)
{
	return CL_Thread(new CL_ThreadFunc_Runnable_v1<Class, Param>(self, func, param), true);
}
*/

#endif