为什么使用std ::从C ++ 11异步? [英] Why using std::async from C++11?

查看:211
本文介绍了为什么使用std ::从C ++ 11异步?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想深入探讨新的C ++ 11个标准的所有选项,而使用std ::异步和阅读它的定义,我注意到两件事情,至少在用gcc 4.8.1 Linux的:

I'm trying to explore all the options of the new C++11 standard in depth, while using std::async and reading its definition, I noticed 2 things, at least under linux with gcc 4.8.1 :


  • 这就是所谓的异步的,但它得到了一个真正的顺序行为,在这里你调用的将来的与您的异步功能相关的富<行基本/ em>的,程序块,直到执行的美孚的它完成/。

  • 这取决于完全相同的外部库为别人,更好的,无阻塞的解决方案,这意味着的pthread ,如果你想使用的std ::异步您需要的pthread。

  • it's called async, but it got a really "sequential behaviour", basically in the row where you call the future associated with your async function foo, the program blocks until the execution of foo it's completed.
  • it depends on the exact same external library as others, and better, non-blocking solutions, which means pthread, if you want to use std::async you need pthread.

在这一点上很自然,我问为什么,甚至上一组简单的函子选择的std ::异步?它甚至不扩展可言,更多的未来,你调用一个解决方案,响应少你的程序会。

at this point it's natural for me asking why choosing std::async over even a simple set of functors ? It's a solution that doesn't even scale at all, the more future you call, the less responsive your program will be.

我缺少的东西吗?你也可以说是授予一个异步被执行的例子,无阻塞,方式是什么?

Am I missing something ? Can you show an example that is granted to be executed in an async, non blocking, way ?

推荐答案

如果你需要一个异步操作的结果,那么你的有无的阻拦,不管你用什么库。这个想法是,你可以选择何时阻止,并希望当你这样做,你阻止一个微不足道的时间,因为所有的工作已经完成。

If you need the result of an asynchronous operation, then you have to block, no matter what library you use. The idea is that you get to choose when to block, and, hopefully when you do that, you block for a negligible time because all the work has already been done.

还要注意的是的std ::异步可与政策推出的std ::推出::异步的std ::推出::递延。如果不指定,则允许在实现中选择,而且它很可能选择使用递延评价,这将导致所有的工作正在做,当你试图从未来得到的结果,从而延长块。所以,如果你想确保这项工作是异步完成后,使用的std ::推出::异步

Note also that std::async can be launched with policies std::launch::async or std::launch::deferred. If you don't specify it, the implementation is allowed to choose, and it could well choose to use deferred evaluation, which would result in all the work being done when you attempt to get the result from the future, resulting in a longer block. So if you want to make sure that the work is done asynchronously, use std::launch::async.

这篇关于为什么使用std ::从C ++ 11异步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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