Jump to content
  • Advertisement
Sign in to follow this  
darkchrono4

Question about multithreading

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

I'm using _beginthread() to create another thread in my program. Don't have any problems getting it to work. My question is how do you pass more then one arguement to the threaded function?

Share this post


Link to post
Share on other sites
Advertisement
Probably the easiest way is to create a structure with the data in it and pass a pointer to the structure.

Share this post


Link to post
Share on other sites
Pass a struct.

struct ThreadParams
{
int nParam;
char chParam;
int nSomeNumber;
};

void ThreadFunc(void* pParam)
{
ThreadParams theParams = *(ThreadParams*)pParam;

// Stuff...
}

// In your code:
ThreadParams theParams;
theParams.nParam = 42;
theParams.chParam = 'x';
theParams.nSomeNumber = 123;
_beginthread(ThreadFunc,(void*)&theParams);
Sleep(50);


Thats pseudo code, becase I can't remember the parameters to _beginthread offhand :P
The Sleep() call is there so your function doesn't exit immediately, and cause the parameters structure to go out of scope. Leaving 50ms (which is a long time really) gives the thread time to start and to copy the parameters.

Share this post


Link to post
Share on other sites
A thread can take arbitrarily long to start. Create the parameter block on the heap. If the thread creation returns a failure value, free the block from the creating function. Otherwise, free the block within the thread itself.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!