Lirik Lirik - 1 year ago 239
C Question

Windows threading: _beginthread vs _beginthreadex vs CreateThread C++

What's a better way to start a thread?

I'm trying to determine what are the advantages/disadvantages of

. 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
)... 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.


OK, thanks for all the info, I've also read in a couple of places that I can't call
if I used
, but if I call
in the thread shouldn't that work? What's the deal there?

Answer Source

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).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download