问题描述
_beginthread
、_beginthreadx
或 CreateThread
有什么更好的线程启动方式?
What's a better way to start a thread, _beginthread
, _beginthreadx
or CreateThread
?
我正在尝试确定 _beginthread
、_beginthreadex
和 CreateThread
的优点/缺点是什么.所有这些函数都返回一个线程句柄给新创建的线程,我已经知道 CreateThread 在发生错误时提供了一些额外的信息(可以通过调用 GetLastError
来检查)......但是什么是我在使用这些功能时应该考虑哪些问题?
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?
我正在使用 Windows 应用程序,因此跨平台兼容性已经不可能了.
I'm working with a windows application, so cross-platform compatibility is already out of the question.
我已经阅读了 msdn 文档,但我无法理解,例如,为什么有人会决定使用 _beginthread 而不是 CreateThread,反之亦然.
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.
干杯!
更新:好的,感谢您提供的所有信息,我还在几个地方读到,如果我使用了 _beginthread()
,我将无法调用 WaitForSingleObject()
,但是如果我在线程中调用 _endthread()
不应该这样吗?那里有什么交易?
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()
是一个原始的 Win32 API 调用,用于在内核级别创建另一个控制线程.
CreateThread()
is a raw Win32 API call for creating another thread of control at the kernel level.
_beginthread()
&_beginthreadex()
是在幕后调用 CreateThread()
的 C 运行时库调用.一旦 CreateThread()
返回,_beginthread/ex()
会负责额外的簿记,以使 C 运行时库可用 &在新线程中保持一致.
_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.
在 C++ 中,您几乎肯定应该使用 _beginthreadex()
,除非您根本不会链接到 C 运行时库(又名 MSVCRT*.dll/.lib).
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).
这篇关于Windows 线程:_beginthread 与 _beginthreadex 与 CreateThread C++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!