Sign in to follow this  

_beginthread() and _endthread() in a C++ class

This topic is 3866 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

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

Share this post


Link to post
Share on other sites

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;
}

Share this post


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

Share this post


Link to post
Share on other sites

This topic is 3866 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