What's a better way to start a thread, _beginthread
, _beginthreadx
or CreateThread
?
I'm trying to determine what are the advantages/disadvantages of _beginthread
, _beginthreadex
and CreateThread
. All of these functions return a thread handle to a newly created thread, I already know that CreateThread provides a little extra information when an error occurs (it can be checked by calling GetLastError
)... but what are some things I should consider when I'm using these functions?
I'm working with a windows application, so cross-platform compatibility is already out of the question.
I have gone through the msdn documentation and I just can't understand, for example, why anybody would decide to use _beginthread instead of CreateThread or vice versa.
Cheers!
Update:
OK, thanks for all the info, I've also read in a couple of places that I can't call WaitForSingleObject()
if I used _beginthread()
, but if I call _endthread()
in the thread shouldn't that work? What's the deal there?
CreateThread()
is a raw Win32 API call for creating another thread of control at the kernel level.
_beginthread()
& _beginthreadex()
are C runtime library calls that call CreateThread()
behind the scenes. Once CreateThread()
has returned, _beginthread/ex()
takes care of additional bookkeeping to make the C runtime library usable & consistent in the new thread.
In C++ you should almost certainly use _beginthreadex()
unless you won't be linking to the C runtime library at all (aka MSVCRT*.dll/.lib).