Sign in to follow this  

SDL_Thread Help

This topic is 3933 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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."<<endl; return 0; } and, here is the error. Video.cpp(45): error C2664: 'SDL_CreateThread' : cannot convert parameter 1 from 'int (void *)' to 'int (__cdecl *)(void *)' Any help is much appreciated. [Edited by - rluck on March 8, 2007 6:17:15 PM]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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;
}


Share this post


Link to post
Share on other sites

This topic is 3933 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this