Jump to content
  • Advertisement
Sign in to follow this  
v0dKA

Beginning multithreading

This topic is 4399 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 just started using multithreading. I'm a bit confused about this: what is the proper way to exit a thread? And how will the process that started the thread know about whether or not a thread is active? I thought ExitThread() or TerminateThread() were the proper way to do this, but it appears MSDN does not advise it. MSDN instead says that the thread itself should return. But how will my main thread know about this? Thanks in advance for the help.

Share this post


Link to post
Share on other sites
Advertisement
When the function executed by the CreateThread() call returns, the thread terminates, though ExitThread() isn't a bad way to end the thread if you don't have any C++ objects live. In general, just use return. The HANDLE returned by CreateThread() can be used with functions like WaitForSingleObject() or WaitForMultipleObjects() which will signal when the thread terminates.

Share this post


Link to post
Share on other sites
I am just wanting to start learning multithreading, can you tell me a good book or reference to beginning multithreading?

Share this post


Link to post
Share on other sites
Also, you could have a variable in the shared memory that is called somewhat along the lines of 'ThreadExit'. Have your thread function check for it's value and once it's set to one ( or to whatever value you'd like to exit on ) return from the thread's function.

edit:
Quote:
But how will my main thread know about this?

That is simple once you have above-mentioned ThreadExit variable in the shared memory block. You just let the thread set the ThreadExit variable to 1 if it exits and thus the main program can check that variable to see if the thread ended. Note, it is not advisable to have two thread read/write a variable in the shared variable at once. I believe you can circumvent that problem with mutexes, but I'm not such an advanced multithreader that I feel safe to explain them ;) I'll leave that honor to someone else.

Share this post


Link to post
Share on other sites
Quote:
Original post by adam23
I am just wanting to start learning multithreading, can you tell me a good book or reference to beginning multithreading?


Advanced Windows

Covers more than multithreading and this edition is old but it's a classic.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!