gpt4 book ai didi

c++ - packaged_task 和 async 有什么区别

转载 作者:太空宇宙 更新时间:2023-11-04 12:38:50 25 4
gpt4 key购买 nike

在使用 C++11 的线程模型时,我注意到

std::packaged_task<int(int,int)> task([](int a, int b) { return a + b; });
auto f = task.get_future();
task(2,3);
std::cout << f.get() << '\n';

auto f = std::async(std::launch::async, 
[](int a, int b) { return a + b; }, 2, 3);
std::cout << f.get() << '\n';

似乎在做完全相同的事情。我知道如果我用 std::launch::deferred 运行 std::async 可能会有很大的不同,但在这种情况下有吗?

这两种方法有什么区别,更重要的是,在哪些用例中我应该使用其中一种方法而不是另一种方法?

最佳答案

实际上,如果您使用相当长的函数,您刚才给出的示例会显示出差异,例如

//! sleeps for one second and returns 1
auto sleep = [](){
std::this_thread::sleep_for(std::chrono::seconds(1));
return 1;
};

打包任务

packaged_task 不会自行启动,您必须调用它:

std::packaged_task<int()> task(sleep);

auto f = task.get_future();
task(); // invoke the function

// You have to wait until task returns. Since task calls sleep
// you will have to wait at least 1 second.
std::cout << "You can see this after 1 second\n";

// However, f.get() will be available, since task has already finished.
std::cout << f.get() << std::endl;

std::async

另一方面,std::asynclaunch::async 将尝试在不同的线程中运行任务:

auto f = std::async(std::launch::async, sleep);
std::cout << "You can see this immediately!\n";

// However, the value of the future will be available after sleep has finished
// so f.get() can block up to 1 second.
std::cout << f.get() << "This will be shown after a second!\n";

缺点

但在尝试对所有内容使用 async 之前,请记住返回的 future 具有特殊的共享状态,这需要 future::~future block :

std::async(do_work1); // ~future blocks
std::async(do_work2); // ~future blocks

/* output: (assuming that do_work* log their progress)
do_work1() started;
do_work1() stopped;
do_work2() started;
do_work2() stopped;
*/

因此,如果您想要真正的异步,则需要保留返回的 future,或者如果情况发生变化您不关心结果:

{
auto pizza = std::async(get_pizza);
/* ... */
if(need_to_go)
return; // ~future will block
else
eat(pizza.get());
}

有关这方面的更多信息,请参阅 Herb Sutter 的文章 async and ~future ,它描述了问题,Scott Meyer 的 std::futures from std::async aren't special ,它描述了见解。另请注意此行为 was specified in C++14 and up , 但也通常在 C++11 中实现。

更多区别

通过使用 std::async,您不能再在特定线程上运行您的任务,其中 std::packaged_task 可以移动到其他线程。

std::packaged_task<int(int,int)> task(...);
auto f = task.get_future();
std::thread myThread(std::move(task),2,3);

std::cout << f.get() << "\n";

此外,在调用 f.get() 之前需要调用一个 packaged_task,否则您的程序将卡住,因为 future 永远不会就绪:

std::packaged_task<int(int,int)> task(...);
auto f = task.get_future();
std::cout << f.get() << "\n"; // oops!
task(2,3);

长话短说

如果你想完成某些事情并且并不关心它们何时完成,请使用 std::async,如果你想包装,请使用 std::packaged_task up 东西以便将它们移动到其他线程或稍后调用它们。或者,引用 Christian :

In the end a std::packaged_task is just a lower level feature for implementing std::async (which is why it can do more than std::async if used together with other lower level stuff, like std::thread). Simply spoken a std::packaged_task is a std::function linked to a std::future and std::async wraps and calls a std::packaged_task (possibly in a different thread).

关于c++ - packaged_task 和 async 有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55268324/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com