本文介绍了C ++ 11 std :: thread vs windows CreateThread的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哪些选项更适合在Visual C ++下创建(和管理)线程:C ++ 11 std :: thread WinAPI 函数(例如 CreateThread _beginthreadex 等),为什么?

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?

推荐答案

可移植性



std :: thread 是C ++ 11标准的新手 - 有了它,你可以在C ++中编写支持C ++ 11的编译器中的可移植代码。你可以感觉到未来

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.

它是基于 boost :: thread ,它支持不支持C ++ 11的旧编译器 - 这使得移植到其他平台更容易。

It is based on boost::thread, which supports older compilers not supporting C++11 - which makes porting to other platforms even easier.

如果您需要使用平台特定技巧,是要走的路。

If you need to use platform specific tricks, std::thread::native_handle is the way to go.

CreateThread 是特定于WinAPI的,这意味着编写非可移植代码。

CreateThread is specific to WinAPI, this implies writing non-portable code. Also, this API is quite old and more inconvenient to use.

WinAPI是一个C API不鼓励现代 。你创建的每一个线程原语,你必须以后手动销毁。

WinAPI is a C API which does not encourage modern C++ good practices. Every threading primitive you create, you must later destroy manually.

这不是C ++ 11中的线程库的情况,这使得更高级的抽象更容易写。虽然 std :: thread 仍然是相当低级的(你 .join() .detach()你的线程,或者线程析构函数将终止你的程序),C ++ 11线程库有 std :: lock_guard 其他锁类支持互斥锁的RAII。

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 :: async

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.

WinAPI只能调用具有特定签名的函数指针 - 这很容易出现与类型安全,对象生命周期和管理不当的内存有关的错误。

WinAPI can only call function pointers with specific signature - which is prone to bugs related to type safety, lifetime of objects and mismanaging memory.

std :: thread 可以呼叫任何可呼叫物件:

std::thread can call any callable object:

// 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 :没有理由使用WinAPI线程作为在新的C ++代码中的主线程机制。

TL;DR: There is no reason to use WinAPI threads as the main threading mechanism in new C++ code.

这篇关于C ++ 11 std :: thread vs windows CreateThread的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 17:37