也许我错过了std::asyncC++ 11中新的正确用法,但是这句话(在cppreference.com上):
如果设置了异步标志(即policy&std :: launch :: async!= 0),则async在单独的执行线程上执行函数f,就好像由std :: thread(f,args ...)生成的一样.除了如果函数f返回一个值或抛出异常,它将存储在可通过std :: future访问的共享状态中,async返回给调用者.
让我觉得我的线程应该立即开始这个声明:
std::async(std::launch::async, MyFunctionObject());
Run Code Online (Sandbox Code Playgroud)
无需等待呼叫std::future::get().这似乎不是这种情况(使用MSVC 13进行编译).如果这不是由这个语句本身触发的,如果我不关心std::future对象的返回值,应该如何触发?
例:
#include <thread>
#include <iostream>
#include <array>
#include <future>
static std::mutex write_mutex;
class Cpp11Threads {
public:
// Function operator for Function-Object
void operator()() {
const int num_threads = 50;
// Static std array
std::array<std::thread*, num_threads> worker_threads;
// Range based
for (std::thread*& thread : worker_threads) {
// Lambda expression
thread = new std::thread(
[] { …Run Code Online (Sandbox Code Playgroud)