Windows 线程-C++
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7846796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Windows Threading-C++
提问by atoMerz
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?
我正在 Windows 上寻找能够随意创建和终止线程的 API。还具有将线程绑定到内核的能力。我在这里介绍了 Win32 线程 API 。
但是,当我查看 MSDN 时,我看到了 _beginthreadex() 和 _endthreadex()。所以我猜每次创建线程时都应该调用 _endthreadex ?
要获得此类问题的答案,我正在寻找有关 Windows 线程的教程。有人能帮忙吗?
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?
PS 这可能是题外话,但 Boost 是否也支持线程关联?如果是这样,有人可以指点我与线程关联相关的教程/文档吗?
回答by Roman R.
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 with TerminateThread
API any time.
创建线程后(例如 with _beginthreadex
),您需要让线程优雅地退出,因为您永远不知道它现在是否处于某些事情的中间(例如,锁定了某个资源)。您仍然可以选择随时使用TerminateThread
API 将其删除。
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.
SetThreadAffinityMask
和朋友让你在CPU战场上定位你的线程。不过,您最终可能会离开操作系统调度程序来选择内核来运行您的线程,因为它很有可能会更有效率。
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:
重用线程的更新:创建一个线程,您正在通过您的线程 proc 启动,一旦您从它返回,该线程即将终止。也就是说,可以通过两种方式启动另一个工作线程活动:从一开始就创建一个新线程,或者不退出线程过程并同步以赶上新的工作线程活动请求。后者可以使用IPC
对象来实现,例如事件:
int ThreadProc()
{
while(true)
{
wait for new event;
if(termination requested) break;
otherwise, on worker activity request, do next requested task;
}
}
Refer to Thread Synchronization for Beginnersfor sample code and description.
有关示例代码和说明,请参阅初学者的线程同步。
回答by Abhinav
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).
如果您使用的是 MFC,则最好使用 CWinThread。您可以非常轻松地向线程发送消息,并且可以从外部控制线程的行为。使用线程的句柄,您可以使用SetThreadAffinityMask为线程提供关联掩码,这将在所需的处理器上调度线程。
回答by RED SOFT ADAIR
1) Do not mix up _beginthread/_beginthreadexand the Win32 API Function CreateThread. These are two different APIs. See Other SO Postfor details.
1) 不要混淆_beginthread/_beginthreadex和 Win32 API 函数CreateThread。这是两个不同的 API。有关详细信息,请参阅其他 SO 帖子。
2) If you use _beginthread/_beginthreadex, _endthread/_endthreadexshould be used for termination
2)如果使用_beginthread/_beginthreadex,终止时应使用_endthread/_endthreadex
3) TerminateThread (and also _endthread) should not be used under normal conditions. See MSDN Post.
3) TerminateThread(还有_endthread)在正常情况下不应该使用。请参阅MSDN 帖子。
4) Functions such as SetThreadAffinityMask, or SetThreadIdealProcessorcan be used to set the core a thread should use.
4) SetThreadAffinityMask或SetThreadIdealProcessor 等函数可用于设置线程应使用的核心。
5) The boost threadingAPI is much more robust and simple. Actually its the base of the C++11 threads.
5) boost线程API 更加健壮和简单。实际上它是 C++11 线程的基础。