草庐IT

dispatch-async

全部标签

c++ - 来自 boost::async() 的 boost::future<> 类型

我从boost::async()得到了意想不到的结果(Boost1.56,Windows:VS2010和VS2012)。#include...autofunc=[](){return123;};autoboostFut=boost::async(func);//boostFut=42;//intentionalerrortorevealdeducedtypeincompilationerror出于某种原因boostFut推导为boost::unique_future而不是boost::unique_future.我做错了什么?注意:在VS2012上,如果我使用std::async(fu

c++ - 我可以检测在编译时使用了哪个标签分派(dispatch)重载吗?

假设我有一个仿函数,它使用标签分派(dispatch)从函数的多个实现中进行选择,如下所示://baseclassforalltags,indicatingthe"default"implementationstructtag_base{};//subclassesfortagsthatmightselectadifferentimplementationstructtag1:tag_base{};structtag2:tag1{};structtag3:tag2{};structfunc{voidoperator()(tag_base){}voidoperator()(tag3){}

c++ - Boost.Asio strand.dispatch() 可以阻塞吗?

我正在试验Boost.Asiostrand对于我正在编写的服务器,我想澄清一些事情。假设我们有SomeClass,其中包含如下内容:voidSomeClass::foo(){strand_.dispatch(boost::bind(&SomeClass::bar,this));}此外,strand有一个io_service,多个线程调用run()。在strand::dispatch()上的文档中我读到它保证通过strand发布或调度的处理程序不会同时执行,如果满足此条件,则处理程序可能在此函数中执行。什么决定了处理程序是否立即执行?在这种多线程的情况下,如果多个io_service线程

c++ - 我什么时候需要使用 std::async(std::launch::async, func()) 而不是 func()?

我不明白使用std::async的原因与std::lauch::async标志,而不是简单地调用传递给std::async的函数.是否有任何特价保证? 最佳答案 根据thedocumentation,是一个只接受函数及其参数的重载。您正在使用接受更具体的策略的重载。async(f,args...)等同于async(std::launch::async|std::launch::deferred,f,args...),其中两个标志的存在将函数是否简单地与当前调用堆栈分离(推迟到第一个非定时等待函数的执行;这称为惰性求值),或者实际上是

c++ - 如何将对象移动到 std::async()?

我需要将一个对象移动到一个异步函数,让另一个函数管理我的资源。但这似乎很难。例如,我想将一个fstream发送到一个异步函数。voidasv(std::ofstreams){//dosomething.}我想:std::ofstreams("afs");std::async(asv,std::move(s));无法编译。但是std::ofstreams("afs");asv(std::move(s));可以编译。我该怎么做? 最佳答案 这才是正确的做法。(实际上没有什么可以添加到答案中)如果您使用Coliru之类的工具对其进行测试,

c++ - 如何使用 yield_context 作为 resolver.async_resolve 的处理程序?

使用yield_context作为堆栈协程中Asio异步操作的处理程序非常棒!但是ip::basic_resolver::async_resolve的处理程序具有与简单地接收错误代码不同的签名(我很好奇为什么它不将resolver::iterator&作为async_resolve中的参数,就像basic_socket&中的basic_socket_acceptor::async_accept参数一样)).有没有办法使用yield作为它的处理者?同样的问题也适用于async_connect. 最佳答案 如StackfulCorout

c++ - io_service::run() async_* 函数的处理顺序

假设在调用io_service::run()时,有多个async_read操作被调度(它们之间可能还有其他操作)。当在ReadHandler函数中安排异步操作(如async_write)时会发生什么?voidhandler(constboost::system::error_code&error,std::size_tbytes){async_write(sock,boost::asio::buffer(wbuf),whandler);}也就是说,什么时候调用async_write?我希望执行顺序是:1)async_read//12)async_write3)async_read//2

c++ - std::async 和 std::future 行为

我试图理解异步行为并编写了一些愚蠢的测试程序。intf(inti){std::cout使用上面的代码,输出似乎是完全同步的。所有10000个线程似乎都按顺序执行。主线程block。0:hello1:hello2:hello.......10000:helloinmain但是,当返回的future存储在vector中时,输出全部被破坏并且main退出而不等待生成的线程。线程是否在此处分离?intmain(){std::vector>v;for(inti=0;i输出:2:hello3:hello46:hello:hello5:hello9:hello10:hello11:hello最后,尝

c++ - 如果从构造函数/析构函数中不加限定地调用虚函数,是否会发生虚分派(dispatch)?

structA{virtual~A(){f();}virtualvoidf(){}};我已将我的问题编辑得更具体..在此代码示例中,调用f()可以使用虚拟分派(dispatch),还是保证等同于A::f()?您能否提供C++标准中的相关部分?谢谢。 最佳答案 在构造函数或析构函数中,子类对象要么尚未构造,要么已经被销毁。因此,虚拟分派(dispatch)不会导致使用派生类版本,而是调用基类版本。根据标准,[class.cdtor]/4:Memberfunctions,includingvirtualfunctions(10.3),c

c++ - boost::asio::async_read_until 问题

我正在尝试修改来自boostasio的echo服务器示例,当我尝试使用boost::asio::async_read_until时遇到了问题。这是代码:#include#include#include#includeusingboost::asio::ip::tcp;classsession{public:session(boost::asio::io_service&io_service):socket_(io_service){}tcp::socket&socket(){returnsocket_;}voidstart(){std::coutsocket(),boost::bind