Skip to main content
gamedev.net
🔒 Locked

SDL_Thread Help

Started by rluck Mar 8, 2007 at 5:59 PM 1 replies 2.5k views
Original Post
rluck
rluck
Just looking for some quick help with SDL_Thread. It doesn't seem to like my code, I've read a whole lot on it and have written it just like the examples. Here is the line where I declare the thread. thread = SDL_CreateThread(playMovie, NULL); <-- this is the line causing the error. Here is the function, whether the SMPEG_play is commented out or not makes no difference. int video::playMovie(void *data) { //SMPEG_play(this->movie); cout<<"Thread worked."<[Edited by - rluck on March 8, 2007 6:17:15 PM]
evillive2
evillive2
Have you tried making video::playMovie a static function of the video class? There is an important distinction between member functions and static member functions when using "function pointers" that I am not qualified to explain but I think may be an issue here. While there is a good tutorial/FAQ on function pointers here I am not sure that it offers you the answers you are looking for.

Perhaps someone better at explaining the issues involved here could help? Fruny is usually offering some really good explanations on topics like this if you want to search the forums to see if he has posted on related topics.
Evillive2
DMINATOR
DMINATOR
Ah yes, this is well known problem. If you want to use member functions, then you have 2 options.

1) Make them static
2) Use a little hack

If you want to use a hack, then you are lucky because there are plenty of topics here on gamedev.net that solve this.

Here is one that helped me allot clicky.

I have modified it a little (original code was made by Null and Void) because my engine requires all the memory to be cleared manually. But choose the best way you like.

#define _STATIC_THREAD_//define _DYNAMIC_THREAD_template <class ThisType, class FuncType>struct MethodInfo {	static int Stub(void *_Info) 	{		//Dynamic thread creates pointer to the functype		//which means it should be destroyed after function end		//however if thread is killed it is not called and memory leak is found		#ifdef _DYNAMIC_THREAD_			MethodInfo <ThisType, FuncType> *Info;					Info = (MethodInfo <ThisType, FuncType> *)(_Info);			((Info->This)->*(Info->Func))();					delete Info;				//the static thread fixes this problem by using variable generated on the		//stack which means it is created and destroyed automaticly		//so no memory leak is found		#else //_STATIC_THREAD_			MethodInfo <ThisType, FuncType> Info;					Info		= *(MethodInfo <ThisType, FuncType> *)(_Info);					delete (MethodInfo <ThisType, FuncType> *)(_Info);			((Info.This)->*(Info.Func))();				#endif				//delete Info;		return 0;	}		ThisType *This;	FuncType Func;		MethodInfo(ThisType *_This, FuncType _F) : This(_This), Func(_F) {}	MethodInfo(){};};template <class ThisType, class FuncType>SDL_Thread* SDL_CreateMemberThread(ThisType *This, FuncType Func) {		MethodInfo <ThisType,FuncType> *Info;		Info = new MethodInfo <ThisType,FuncType> (This,Func);		return SDL_CreateThread(( int(*)(void *) )(MethodInfo<ThisType,FuncType>::Stub),Info);}



This is how I call my worker member thread function. I have created a very easy to use thred class for this.
void Thread::Start(){   SDL_Thread* _thread = SDL_CreateMemberThread( this,  &Thread::WorkerChecker );}int Thread::WorkerChecker(){      //your thread code here!	return 1;}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.