Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 07-23-2006, 11:22 AM   #1
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
passing a class member function to pthread_create.

I was wondering if I could create a simple conveter to pass a class member
function to pthread_create and the following is what I have come up with.
I can't see any problems with it (yes I know there are memory leaks),
but I'm wondering if anybody else can see any.

Sorry for the long post

A struct for storing the Object pointer(o) and a function parameter(p)
T is a pointer the class
P is a pointer to the parameter which is passed to the class memeber function 
template<typename T,typename P>
struct OP
	T o;
	P p;


//normal posix pthread C function pointer
typedef void* (*func_ptr) (void*);

posix class member function starter
R is the return type of the member function
T is the type of class which the function is a member function of
P is the member functions parameter- NOTE THIS IS A POINTER we are dealing with 
  	 passing information between threads
R(T::*f)(P) is the signture of the function pointer
void* op is the templates struct above OP<T,P>
template<class R,class T,class P, R (T::*f)(P)>
void* thread_starter(void* op)
	//get and store the class pointer
	T* p_class = static_cast<T*>(static_cast<OP<T*,P>*>(op)->o);
	//get and store the parameter
	P parm = static_cast<P>(static_cast<OP<T*,P>*>(op)->p);
	//delete the struct which contains the class pointer and parameter
	delete static_cast<OP<T*,P>*>(op);
	//call the class member function passing the parameter
	return 0;

Class : Thread
Remarks :A simple inlined wrapper for pthread
class Thread
	Thread( void ){;}
	~Thread( void ){;}
	inline bool start( func_ptr func, void* data)
		if( pthread_create( &m_thread, NULL, func, data ) != 0){ return false; }
		return true;

template<class R, class T,class P, R (T::*mem_func)(P)>
inline bool start_member(T *object, P param)
	 OP<T*,P>* op = new OP<T*,P>;
	if( pthread_create( &m_thread, NULL, thread_starter<R,T,P,mem_func>, static_cast<void*>(op) ) != 0)
		{ return false; }
	return true;

inline bool join( void )
	if(pthread_join( m_thread, NULL ) != 0)
	{ return false; }
	return true;	
inline bool close_thread( void )
	int result = pthread_cancel(m_thread);
	if (result != 0){ return false; }
	return true;
inline void exit_caller( void )
	int exit_code = -1;
	pthread_t m_thread;

//tester class
class Some
	int func1(int* a){return int;}
	void func2(char*c){return;}
	void func3(float* d){return;}

int main(int argc, char* argv[ ])
	thread = new Thread;
	Some* class_p = new Some;
	int * int_p = new int;
	char* char_p  = new char;
	*char_p = 'C';

	//call: int Some::func1(int*)

	or calling: void Some::func2(char*)
	would be done like:

	return 0;

Last edited by dmail; 07-23-2006 at 01:07 PM. Reason: typo error
Old 07-29-2006, 11:15 AM   #2
Registered: Oct 2005
Posts: 970

Original Poster
Rep: Reputation: Disabled
If anybody is interested the sources for this are located @
Thread starters
Posix thread
Win32 thread

Last edited by dmail; 07-29-2006 at 11:17 AM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
passing arg 3 of `pthread_create' from incompatible pointer type wallwaters Programming 8 03-08-2010 01:46 AM
C++ / Passing a variable by reference to a class member function. sepulture Programming 12 11-15-2005 10:23 PM
What is the warning: passing arg 3 of `pthread_create' from incompatible pointer type wallwaters Linux - Software 3 06-01-2005 08:30 AM
A main can be changed by a function local without passing anything to the function? ananthbv Programming 10 05-04-2004 01:31 PM
a class with a ifstream member Hano Programming 2 04-24-2002 11:01 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:29 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration