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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:16:49  来源:igfitidea点击:

C++11 std::thread vs windows CreateThread

c++windowsmultithreadingvisual-c++

提问by KaMyLuS

Which option is better for creating (and managing) threads under Visual C++: C++11 std::threador WinAPIfunctions (such as CreateThread, _beginthreadex, etc.) and why?

哪个选项是在Visual C ++创建(和管理)的线程更好的:C ++ 11std::threadWinAPI功能(如CreateThread_beginthreadex等),为什么?

回答by milleniumbug

Portability

可移植性

std::threadis new to C++11 standard - with it, you can write portable code in C++ across compilers supporting C++11. You can feel the futurein 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_handleis the way to go.

如果您需要使用特定于平台的技巧,这std::thread::native_handle是要走的路。

CreateThreadis 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::threadis 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_guardand 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::asyncfor 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::threadcan 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::threadoffers RAII-guarantees as to the cleanup of the thread, and supports arbitrary function object arguments instead of just function pointers. std::threadis 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::threadand somehow even worseCreateThreadEx. std::threadis like, "Hey guys, I offered you cross-platform mmap, so you could write your own mallocon 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::threadis 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::threadis the way to go.

除非您只针对 Windows 并且您需要使用 WinAPI 与您的线程进行交互,否则就是std::thread要走的路。