我有点被std::async函数搞糊涂了。
说明书上说:
异步操作正在被执行,就像在一个新的执行线程中一样(C++11§30.6.8/11)。
这是什么意思?
据我理解,代码
std::future<double> fut = std::async(std::launch::async, pow2, num);应该在新线程上启动函数pow2,并通过值将变量num传递给线程,然后在以后的某个时候,当函数完成时,将结果放在fut中(只要函数pow2有类似double pow2(double);的签名)。但是说明书上写着“好像”,这让整个事情对我来说有点模糊。
问题是:
是一个总是在这种情况下启动的新线程?,我希望如此。我的意思是,参数std::launch::async在某种程度上是有意义的,我明确地表示,我确实希望创建一个新线程。
和密码
std::future<double> fut = std::async(std::launch::deferred, pow2, num);应该通过将函数调用推迟到编写类似于var = fut.get();的内容,从而使pow2懒惰评估成为可能。在这种情况下,参数std::launch::deferred,应该意味着我显式地声明,我不想要一个新线程,我只想确保函数在需要它的返回值时被调用。
我的假设正确吗?如果没有,请解释。
此外,我知道默认情况下函数的调用如下:
std::future<double> fut = std::async(std::launch::deferred | std::launch::async, pow2, num);在这种情况下,我被告知是否启动一个新线程取决于实现。再说一遍,,这是什么意思?
发布于 2015-09-11 06:45:51
std::async ( <future>标头的一部分)函数模板用于启动(可能)异步任务。它返回一个std::future对象,该对象将最终保存std::async参数函数的返回值。
当需要值时,我们在std::future实例上调用get();这会阻塞线程,直到将来做好准备,然后返回值。可以将std::launch::async或std::launch::deferred指定为std::async的第一个参数,以指定任务如何运行。
std::launch::async表示函数调用必须在它自己的(新)线程上运行。(考虑到用户@T.C.的评论)。std::launch::deferred表示函数调用将推迟到将来调用wait()或get()。在这种情况发生之前,可以将未来的所有权转移到另一个线程。std::launch::async | std::launch::deferred表示实现可以选择。这是默认选项(当您没有自己指定一个选项时)。它可以决定同步运行。是一个总是在本例中启动的新线程?。
从1.中,我们可以说总是会启动一个新线程。
是我对std::std::延迟的假设,对吗?
从2.,我们可以说你的假设是正确的。
,这是什么意思?与正在启动或不启动的新线程有关,取决于实现
在3.中,由于std::launch::async | std::launch::deferred是默认选项,这意味着模板函数std::async的实现将决定它是否将创建一个新线程。这是因为有些实现可能会检查过度调度。
警告
以下部分与你的问题无关,但我认为重要的是要记住这一点。
C++标准指出,如果std::future保存与调用异步函数对应的共享状态的最后引用,则std::未来的析构函数必须阻塞,直到异步运行函数的线程完成为止。因此,由std::future返回的std::async实例将在其析构函数中阻塞。
void operation()
{
auto func = [] { std::this_thread::sleep_for( std::chrono::seconds( 2 ) ); };
std::async( std::launch::async, func );
std::async( std::launch::async, func );
std::future<void> f{ std::async( std::launch::async, func ) };
}这种误导性代码会让您认为std::async调用是异步的,它们实际上是同步的。由std::future返回的std::async实例是临时的,并且将被阻塞,因为当std::async返回时,它们的析构函数被正确调用,因为它们没有分配给一个变量。
第一个对std::async的调用将阻塞2秒,然后从第二个对std::async的调用中再阻塞2秒。我们可能认为对std::async的最后一次调用不会阻塞,因为我们将返回的std::future实例存储在变量中,但由于它是在作用域结束时被销毁的局部变量,因此在函数的作用域结束时,当局部变量f被销毁时,它实际上会阻塞2秒。
换句话说,调用operation()函数将阻塞同步调用的任何线程,时间大约为6秒。在C++标准的未来版本中可能不存在这样的需求。
我用来编译这些注释的信息源:
C++并发在行动:实用多线程,安东尼威廉姆斯
Scott的博客文章:http://scottmeyers.blogspot.ca/2013/03/stdfutures-from-stdasync-arent-special.html
发布于 2017-03-16 11:08:26
我也对此感到困惑,并在Windows上运行了一个快速测试,表明异步的未来将在OS线程池线程上运行。一个简单的应用程序可以演示这一点,Visual中的突出部分还将显示名为"TppWorkerThread“的执行线程。
#include <future>
#include <thread>
#include <iostream>
using namespace std;
int main()
{
cout << "main thread id " << this_thread::get_id() << endl;
future<int> f1 = async(launch::async, [](){
cout << "future run on thread " << this_thread::get_id() << endl;
return 1;
});
f1.get();
future<int> f2 = async(launch::async, [](){
cout << "future run on thread " << this_thread::get_id() << endl;
return 1;
});
f2.get();
future<int> f3 = async(launch::async, [](){
cout << "future run on thread " << this_thread::get_id() << endl;
return 1;
});
f3.get();
cin.ignore();
return 0;
}将产生类似于以下内容的输出:
main thread id 4164
future run on thread 4188
future run on thread 4188
future run on thread 4188发布于 2018-02-09 12:52:37
这并不是真的。添加thread_local存储值,您将看到,实际上std::async run f1 f2 f3任务位于不同的线程中,但具有相同的std::thread::id。
https://stackoverflow.com/questions/30810305
复制相似问题