_beginthread() and _endthread() in a C++ class
Hi,
I'm working on a sound manager library, and i'm tryng to create a thread inside the library function, so that sound updates and done inside that thread.
However, i'm having compiler errors, and i'm not sure what to do about them.
First of all, my sound library is initialized, and the thread for it created :
bool Sound_Manager::Initialize()
{
alutInit(NULL,0);
if(alGetError() == AL_NO_ERROR)
{
music_Thread = (HANDLE)_beginthread((void)this->Update_All,0,0 );
}
else initialized = 0;
return initialized;
}
update all, its just a function that i want to call to update all my sounds\streaming,etc
void Sound_Manager::Update_All()
{
this->Update_All_Sounds();
Update_All();
return;
}
However this is not working.., is there a way to create a thread inside a class, and being able to update it inside the own class ??
thanks,
Bruno
class Sound_Manager{ static void Update_Al(void* p);};f(alGetError() == AL_NO_ERROR){ music_Thread = (HANDLE)_beginthread(Sound_Manager::Update_All,0,this );} void Sound_Manager::Update_All(void* p){ Sound_Manager* ps = (Sound_Manager*)p; ps->Update_All_Sounds(); //Update_All(); return;}
Or in other words, your thread function needs to be a static member of your class. Which would mean, for example, that it won't have a this pointer.
On a related note, your function signature for Update_All is wrong. beginthread needs a function that returns void and takes a single void* parameter. You have a function that takes no parameters. The fix would be to pass 'this' as the third parameter to beginthread so that your thread function can get at it as gyula demonstrated.
Depending on your compiler settings you may also need to add __cdecl to the declaration of Update_All.
As a rule of thumb, casting function pointers is a *really* easy way to get into trouble. If you hadn't of done that then most of the other problems would probably have been found by the compiler.
On a related note, your function signature for Update_All is wrong. beginthread needs a function that returns void and takes a single void* parameter. You have a function that takes no parameters. The fix would be to pass 'this' as the third parameter to beginthread so that your thread function can get at it as gyula demonstrated.
Depending on your compiler settings you may also need to add __cdecl to the declaration of Update_All.
As a rule of thumb, casting function pointers is a *really* easy way to get into trouble. If you hadn't of done that then most of the other problems would probably have been found by the compiler.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement