草庐IT

completable-future

全部标签

c++ - 有效 future 与默认构建 future

我正在并发编程课上学习futures。我的教授在她的幻灯片中说明了这一点:"Valid"futuresare future objectsassociatedtoa sharedstate,andareconstructedbycallingoneofthefollowingfunctions:asyncpromise::get_futurepackaged_task::get_futurefuture objectsareonlyusefulwhentheyare valid. Default-constructed future objectsarenot valid (unles

c++ - 如何将异常报告给 boost::future?

如果我使用Boostfutures,并且future报告true到has_exception(),是否有任何方法可以检索该异常?例如,这里是下面的代码:intdo_something(){...throwsome_exception();...}...boost::packaged_tasktask(do_something);boost::unique_futurefi=task.get_future();boost::threadthread(boost::move(task));fi.wait();if(fi.has_exception()){boost::rethrow_exc

c++ - 如何将 std::future<T> 转换为 std::future<void>?

我的情况是std::future由对APIA的调用产生,但需要为APIB提供std::future:std::futureapi_a();voidapi_b(std::future&depend_on_this_event);在没有建议功能的情况下,例如.then()或when_all(),是否有任何有效的方法可以丢弃附加到std::future的值只剩下底层std::future代表事件完成?像下面这样的东西可以工作但可能效率低下:autof=api_a();f.wait();autovoid_f=std::async(std::launch::defer,[]{});api_b(v

c++ - 删除 .. 在 boost filesystem::complete

这应该很简单:我正在使用boost文件系统编写绝对路径,但它返回的路径非常丑陋:D:/Projects/SomeDir/vc10/../resource/plugins/SomeFile.dll是否有自动处理.'s和..'s的boost方法?我所做的是:boost::filesystem::complete("../resource/plugins/SomeFile.dll")它使用boost::filesystem2。 最佳答案 boost::filesystem::complete("../resource/plugins/So

c++ - 为什么我不能在 std::future 参数中使用引用

为什么以下代码(onIdeone)会给我一个错误?#include#include#includeintmain(){intfoo=0;boolbar=false;std::futureasync_request=std::async(std::launch::async,[=,&foo](bool&is_pumping_request)->std::string{return"str";},bar);std::cout输出:Infileincludedfrom/usr/include/c++/5/future:38:0,fromprog.cpp:1:/usr/include/c++/

c++ - 为什么 boost::future<T>::then() 产生一个新线程?

将延续附加到boost::future时,延续在新线程中执行:std::coutp;boost::futuref=p.get_future();p.set_value();boost::futuref2=f.then([](boost::future){std::cout这段代码输出:main:0x7fff7a8d7310future:0x101781000为什么.then()允许这样做,更重要的是,有没有办法自定义这种行为?从promise/packaged_task/async返回的future是否有不同的行为? 最佳答案 @i

C++ 11 future_status::deferred 不工作

#include#include#includeusingnamespacestd;usingnamespacestd::chrono;intsampleFunction(inta){returna;}intmain(){futuref1=async(launch::deferred,sampleFunction,10);future_statusstatusF1=f1.wait_for(seconds(10));if(statusF1==future_status::ready)cout在上面的示例中,我期望future_status被deferred而不是timeout。sampl

c++ - 当 std::future 准备好被检索时向主线程发送信号

我正在尝试理解std::async、std::future系统。我不太明白的是,您如何处理运行多个异步“任务”,然后根据第一个、第二个等返回的内容运行一些额外的代码。示例:假设您的主线程处于一个简单的循环中。现在,根据用户输入,您通过std::async运行多个函数,并将future保存在std::list中。我的问题是,如何从std::async函数传回可以指定哪个future完成的信息?我的主线程基本上处于消息循环中,我需要做的是让std::async运行的函数能够对消息进行排队,该消息以某种方式指定哪个future是完整的。问题是该函数无法访问future。我是不是漏掉了什么?这

c++ - 组合返回 future 的功能

假设我有两个返回future的函数:std::futurefoo(int);std::futurebar(Tconst&);我想将这两个函数组合成一个接受int的函数作为参数并返回std::future.这个函数应该怎么写?是否可以为返回futures的函数概括函数组合?std::futurefoobar1(intx){autofoo_x=foo(x);returnbar(foo_x.get());}此函数将阻塞直到foo返回的future完成了吧?这显然不是我想要的。std::futurefoobar2(intx){returnstd::async([=](){autofoo_x=f

c++ - 如何终止 std::future?

我对C++11std::future的一些事情感到困惑。我想动态平衡工作负载,所以如果有一些处理器空闲,我创建一个std::future和std::async来划分剩余的数据。它工作正常。std::futuref[MAX_CHILD];for(eachdataitem){if(found_idle_processor)f[i]=std::async(...);process();}//Atlast,querytheresultoff.for(eachfuture)hold=f[i].get();但有时,一旦找到一些特殊数据项,所有其他数据将被丢弃,程序应立即给出最终结果,然后启动另一个