C++11 std::thread 与 Windows CreateThread
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25221247/
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
C++11 std::thread vs windows CreateThread
提问by KaMyLuS
Which option is better for creating (and managing) threads under Visual C++: C++11 std::thread
or WinAPI
functions (such as CreateThread
, _beginthreadex
, etc.) and why?
哪个选项是在Visual C ++创建(和管理)的线程更好的:C ++ 11std::thread
或WinAPI
功能(如CreateThread
,_beginthreadex
等),为什么?
回答by milleniumbug
Portability
可移植性
std::thread
is new to C++11 standard - with it, you can write portable code in C++ across compilers supporting C++11. You can feel the future
in it.
std::thread
是 C++11 标准的新特性 - 有了它,您可以跨支持 C++11 的编译器用 C++ 编写可移植代码。你能感觉到future
它的存在。
It is based on boost::thread
, which supports older compilers not supporting C++11 - which makes porting to other platforms even easier.
它基于boost::thread
,它支持不支持 C++11 的旧编译器 - 这使得移植到其他平台更加容易。
If you need to use platform specific tricks, std::thread::native_handle
is the way to go.
如果您需要使用特定于平台的技巧,这std::thread::native_handle
是要走的路。
CreateThread
is specific to WinAPI, this implies writing non-portable code. Also, this API is quite old and more inconvenient to use.
CreateThread
特定于 WinAPI,这意味着编写不可移植的代码。另外,这个API已经很老了,使用起来也比较不方便。
RAII
RAII
WinAPI is a C API which does not encourage modern C++goodpractices. Every threading primitive you create, you must later destroy manually.
WinAPI 是不鼓励现代C++良好实践的 C API 。您创建的每个线程原语,您必须稍后手动销毁。
This is not the case for thread library in C++11, and this makes higher-level abstractions easier to write. While std::thread
is still fairly low-level (either you .join()
or .detach()
your thread, or the thread destructor will terminate your program), C++11 threading library has std::lock_guard
and other lock classes for supporting RAII for mutexes.
C++11 中的线程库不是这种情况,这使得更高级的抽象更容易编写。虽然std::thread
仍然相当低级(您.join()
或.detach()
您的线程,或者线程析构函数将终止您的程序),但 C++11 线程库具有std::lock_guard
和其他锁类来支持互斥锁的 RAII。
While C++11 has some higher-level abstractions, like std::async
for launching functions asynchronously, it does not provide other abstractions like threadpools, so you may want to use other libraries.
虽然 C++11 有一些更高级别的抽象,比如std::async
异步启动函数,但它没有提供其他抽象,比如线程池,所以你可能想要使用其他库。
Type safety
类型安全
WinAPI can only call function pointers with specific signature - which is prone to bugs related to type safety, lifetime of objects and mismanaging memory.
WinAPI 只能调用具有特定签名的函数指针——这很容易出现与类型安全、对象生命周期和内存管理不善相关的错误。
std::thread
can call any callable object:
std::thread
可以调用任何可调用对象:
// call free-standing function in a separate thread
std::thread first(func);
// call free-standing function with arguments (1, 2), in a separate thread
std::thread second(func, 1, 2);
// call static member function in a separate thread
std::thread third(&A::static_memfun);
// call non-static member of a temporary in a separate thread
std::thread fourth(&A::memfun, A());
//call std::function in a separate thread
std::function<void(int)> callback = std::bind(func, 1, _1);
std::thread fifth(callback, 2);
// call a function object
Functor f;
std::thread sixth(f);
TL;DR: There is no reason to use WinAPI threads as the main threading mechanism in new C++ code.
TL;DR:没有理由在新的 C++ 代码中使用 WinAPI 线程作为主要线程机制。
回答by Puppy
Cross-platformity is a small benefit. The real benefit is in the interface. std::thread
offers RAII-guarantees as to the cleanup of the thread, and supports arbitrary function object arguments instead of just function pointers. std::thread
is the C++11 wrapper on CreateThreadEX and it is that way for a reason.
跨平台性是一个小好处。真正的好处在于界面。std::thread
为线程的清理提供 RAII 保证,并支持任意函数对象参数,而不仅仅是函数指针。std::thread
是 CreateThreadEX 上的 C++11 包装器,这是有原因的。
Just as a side note, std::thread is a terrible, terrible API. If you're creating threads yourself, you're probably doing it wrong. Use a real threading API like Intel's TBB or Microsoft's PPL, which are vastlysuperior to the terriblestd::thread
and somehow even worseCreateThreadEx. std::thread
is like, "Hey guys, I offered you cross-platform mmap
, so you could write your own malloc
on top, enjoy!".
顺便提一下,std::thread 是一个非常糟糕的 API。如果你自己创建线程,你可能做错了。使用一个真正的线程API像英特尔TBB或微软的PPL,这是远远优于可怕的std::thread
和不知何故更糟CreateThreadEx。std::thread
就像,“嘿伙计们,我为您提供了跨平台mmap
,所以您可以malloc
在上面编写自己的,享受!”。
回答by Puppy
You should probably use std::thread
.
您可能应该使用std::thread
.
std::thread
is part of the (new) standard, and is portable.
std::thread
是(新)标准的一部分,并且是可移植的。
Unless you're only targeting Windows AND you need to interact with your threads using the WinAPI, std::thread
is the way to go.
除非您只针对 Windows 并且您需要使用 WinAPI 与您的线程进行交互,否则就是std::thread
要走的路。