Windows 线程-C++
我正在寻找 Windows 上的 API,可以随意创建和终止线程。还能够将线程绑定到核心。 此处向我介绍了 Win32 线程 API。
然而,当我检查 MSDN 时,我看到 _beginthreadex() 和 _endthreadex()。所以我猜每次创建线程时都应该调用 _endthreadex ?
为了获得此类问题的答案,我正在寻找有关 Windows 线程的教程。谁能帮忙解决这个问题吗?
PS 这可能是题外话,但是 Boost 也支持线程关联吗?如果是这样,有人可以向我指出与线程亲和力相关的教程/文档吗?
I'm looking for an API on windows that enables to to create and kill threads at will. Also having ability to bind threads to cores. I was introduced to Win32 Threading API here.
However when I checked MSDN I see _beginthreadex(), and _endthreadex(). So I'm guessing there should be a call to _endthreadex everytime I create a thread?
To get answers to such questions I'm looking for a tutorial on Windows Threading. Can anyone help with this?
P.S. This may be off topic, but does Boost support thread affinity too? If so, can someone point me to a tutorial/documentation related to thread affinity?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
创建线程(例如使用
_beginthreadex
)后,您需要让线程优雅地退出,因为您永远不知道它现在是否正在执行某些操作(例如锁定某个资源) 。您仍然可以随时使用TerminateThread
API 来消除它。SetThreadAffinityMask
和朋友让你在CPU战场上定位你的线程。不过,您最终可能会让操作系统调度程序选择运行线程的核心,因为它很有可能会更高效。关于重用线程的更新:创建一个线程,您将传递线程过程来启动,一旦您从它返回,该线程即将终止。也就是说,可以通过两种方式启动另一个工作线程活动:要么从头开始创建一个新线程,要么不从线程过程中退出并同步以赶上新的工作活动请求。后者可以使用 < code>IPC 对象,例如事件:
参考初学者线程同步示例代码和说明。
Having thread created (such as with
_beginthreadex
) you need to let the thread exit gracefully as you never know if it is in the middle of something just now (having a lock on a certain resource - for instance). Still you have an option to blow it away withTerminateThread
API any time.SetThreadAffinityMask
and friends let you locate your threads at the CPU battlefield. You might end up leaving OS scheduler to choose cores to run your threads on though, as chances are high that it is going to be more efficient.Update on reusing threads: Creating a thread you are passing your thread proc to start, and as soon as you return from it, the thread is about to be terminated. That is, starting another worker thread activity is possible in two ways: either create a new thread from the start, or do not exit from thread proc and synchronize to catch up a new worker activity request. The latter might be implemented using
IPC
objects, e.g. events:Refer to Thread Synchronization for Beginners for sample code and description.
如果你使用MFC,你可以更好地使用CWinThread。您可以非常轻松地向线程发送消息,并且可以从外部控制线程的行为。使用线程的句柄,您可以使用SetThreadAffinityMask为线程提供亲和性掩码,这将在所需的处理器上调度线程。
If you are using MFC, you can better use CWinThread. You can send messages to the thread very easily and can control the thread's behaviour from outside. Using the thread's handle, you can provide an affinity mask for a thread using SetThreadAffinityMask, which will schedule a thread on desired processor(s).
1) 不要混淆 _beginthread/_beginthreadex< /a> 和 Win32 API 函数 创建线程。这是两个不同的 API。有关详细信息,请参阅其他 SO 帖子。
2) 如果使用 _beginthread/_beginthreadex,_endthread /_endthreadex 应该用于终止
3) TerminateThread(以及 _endthread)在正常情况下不应该使用。请参阅 MSDN 帖子。
4) SetThreadAffinityMask 等函数 或 SetThreadIdealProcessor 可以使用设置线程应使用的核心。
5) boost 线程 API 更加健壮而且简单。实际上它是 C++11 线程的基础。
1) Do not mix up _beginthread/_beginthreadex and the Win32 API Function CreateThread. These are two different APIs. See Other SO Post for details.
2) If you use _beginthread/_beginthreadex, _endthread/_endthreadex should be used for termination
3) TerminateThread (and also _endthread) should not be used under normal conditions. See MSDN Post.
4) Functions such as SetThreadAffinityMask, or SetThreadIdealProcessor can be used to set the core a thread should use.
5) The boost threading API is much more robust and simple. Actually its the base of the C++11 threads.