标准C ++ 11是否保证std :: async(std :: launch :: async,func)在单独的线程中启动func? [英] Does standard C++11 guarantee that std::async(std::launch::async, func) launches func in separate thread?

查看:111
本文介绍了标准C ++ 11是否保证std :: async(std :: launch :: async,func)在单独的线程中启动func?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标准C ++ 11是否保证std::async(std::launch::async, func)在单独的线程中启动功能?

Does standard C++11 guarantee that std::async(std::launch::async, func) launches function in separate thread?

编程语言C ++标准工作草案2016-07-12: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/n4606.pdf

Working Draft, Standard for Programming Language C++ 2016-07-12: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/n4606.pdf

1.一方面,C ++ 11-Standard指出,如果无法创建线程,则会出现错误.这样可以确保在没有错误的情况下创建新线程.

1. On the one hand, C++11-Standard says that if the thread can not be created, then there is an error. This ensures the creation of a new thread (in the absence of errors).

§30.6.8

§ 30.6.8

6

抛出:system_error if policy == launch :: async和 实现无法启动新线程.

Throws: system_error if policy == launch::async and the implementation is unable to start a new thread.

7个错误情况:

(7.1)— resource_unavailable_try_again —如果策略== launch :: async 并且系统无法启动新线程.

(7.1) — resource_unavailable_try_again — if policy == launch::async and the system is unable to start a new thread.

文档说明: http://en.cppreference.com/w/cpp/线程/启动

std :: launch :: async启动新线程以执行任务 异步

std::launch::async a new thread is launched to execute the task asynchronously

2.另一方面,写道,可以潜在地创建线程.这些,不必创建线程.

2. On the other hand, it is written that the thread can be potentially created. Those, is not necessary that thread should be created.

§30.6.8

§ 30.6.8

1函数模板异步提供了一种启动程序的机制. 函数可能在新线程中,并提供 在将来与之共享共享状态的对象中起作用.

1 The function template async provides a mechanism to launch a function potentially in a new thread and provides the result of the function in a future object with which it shares a shared state.

这里写的就像在新线程中一样,这是否意味着在新的单独线程中没有必要?

And here written as if in a new thread, does it mean not necessary in new separate thread?

§30.6.8

§ 30.6.8

(3.1)

-如果政策& launch :: async为非零-调用INVOKE(DECAY_COPY (std :: forward(f)),DECAY_COPY(std :: forward(args))...) (20.14.2,30.3.1.2)就像在新线程中一样 由线程对象表示,对DECAY_COPY()的调用为 在调用异步的线程中求值.任何返回值都被存储 结果处于共享状态.从 执行INVOKE(DECAY_COPY(std :: forward(f)),DECAY_COPY (std :: forward(args))...)作为例外结果存储在 共享状态.线程对象以共享状态存储,并且 影响任何引用的异步返回对象的行为 该状态.

— if policy & launch::async is non-zero — calls INVOKE (DECAY_COPY (std::forward(f)), DECAY_COPY (std::forward(args))...) (20.14.2, 30.3.1.2) as if in a new thread of execution represented by a thread object with the calls to DECAY_COPY () being evaluated in the thread that called async. Any return value is stored as the result in the shared state. Any exception propagated from the execution of INVOKE (DECAY_COPY (std::forward(f)), DECAY_COPY (std::forward(args))...) is stored as the exceptional result in the shared state. The thread object is stored in the shared state and affects the behavior of any asynchronous return objects that reference that state.

使用std::async(std::launch::async, func)时,标准C ++ 11是否保证func()将在单独的线程中执行,或者可以在称为异步的同一线程中执行?

When used std::async(std::launch::async, func) then does standard C++11 guarantee that func() will be executed in separate thread, or it can be executed in the same thread that called async?

推荐答案

此处的两个关键声明是:

The two key statements here are:

就像在由thread对象表示的新执行线程中一样

as if in a new thread of execution represented by a thread object

thread对象以共享状态存储,并且会影响引用该状态的所有异步返回对象的行为.

The thread object is stored in the shared state and affects the behavior of any asynchronous return objects that reference that state.

好像"表示它的行为必须与为该函数创建了std::thread对象一样.这意味着创建std::thread的所有副作用也必须发生.

"As if" means it must behave exactly as if it had created a std::thread object for this function. Which means that all side effects of the creation of a std::thread must also happen.

也就是说,如果将launch::asynclaunch::deferred结合使用,则实现将决定是启动新线程还是将其推迟到现有线程中.因此,只有launch::async一个需要新线程.

That being said, if you combine launch::async with launch::deferred, then the implementation decides whether to launch a new thread or defer it to an existing one. So it's only launch::async alone that requires a new thread.

这篇关于标准C ++ 11是否保证std :: async(std :: launch :: async,func)在单独的线程中启动func?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆