Beginning multithreading

Started by
4 comments, last by LessBread 17 years, 8 months ago
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.
.:<<-v0d[KA]->>:.
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.
I am just wanting to start learning multithreading, can you tell me a good book or reference to beginning multithreading?
Adamhttp://www.allgamedevelopment.com
This book has some info about threads starting at page 1000.
Its not much but it is a start
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.
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.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man

This topic is closed to new replies.

Advertisement