草庐IT

future-swap

全部标签

c++ - 有没有办法在 C++11 中取消/分离 future ?

我有以下代码:#include#include#include#includeusingnamespacestd;intsleep_10s(){this_thread::sleep_for(chrono::seconds(10));cout这应该等待1秒,打印“超时”,然后退出。它没有退出,而是再等待9秒,打印“SleepingDone”,然后出现段错误。有没有办法取消或分离future,所以我的代码将在main结束时退出,而不是等待future完成执行? 最佳答案 C++11标准不提供取消以std::async开始的任务的直接方法

c++ - 有没有办法在 C++11 中取消/分离 future ?

我有以下代码:#include#include#include#includeusingnamespacestd;intsleep_10s(){this_thread::sleep_for(chrono::seconds(10));cout这应该等待1秒,打印“超时”,然后退出。它没有退出,而是再等待9秒,打印“SleepingDone”,然后出现段错误。有没有办法取消或分离future,所以我的代码将在main结束时退出,而不是等待future完成执行? 最佳答案 C++11标准不提供取消以std::async开始的任务的直接方法

c++ - iter_swap() 与 swap() - 有什么区别?

MSDNsays:swapshouldbeusedinpreferencetoiter_swap,whichwasincludedintheC++Standardforbackwardcompatibility.但是comp.std.c++says:MostSTLalgorithmsoperateoniteratorranges.Itthereforemakessensetouseiter_swapwhenswappingelementswithinthoseranges,sincethatisitsintendedpurpose---swappingtheelementspointe

c++ - iter_swap() 与 swap() - 有什么区别?

MSDNsays:swapshouldbeusedinpreferencetoiter_swap,whichwasincludedintheC++Standardforbackwardcompatibility.但是comp.std.c++says:MostSTLalgorithmsoperateoniteratorranges.Itthereforemakessensetouseiter_swapwhenswappingelementswithinthoseranges,sincethatisitsintendedpurpose---swappingtheelementspointe

c++ - std::vector::swap 是否使迭代器无效?

如果我交换两个vector,它们的迭代器会保持有效,现在只是指向“其他”容器,还是迭代器会失效?也就是说,给定:usingnamespacestd;vectorx(42,42);vectory;vector::iteratora=x.begin();vector::iteratorb=x.end();x.swap(y);//aandbstillvalid?Pointingtoxory?std似乎对此只字未提:[n3092-23.3.6.2]voidswap(vector&x);Effects:Exchangesthecontentsandcapacity()of*thiswiththa

c++ - std::vector::swap 是否使迭代器无效?

如果我交换两个vector,它们的迭代器会保持有效,现在只是指向“其他”容器,还是迭代器会失效?也就是说,给定:usingnamespacestd;vectorx(42,42);vectory;vector::iteratora=x.begin();vector::iteratorb=x.end();x.swap(y);//aandbstillvalid?Pointingtoxory?std似乎对此只字未提:[n3092-23.3.6.2]voidswap(vector&x);Effects:Exchangesthecontentsandcapacity()of*thiswiththa

c++ - 可以定义一个完全通用的 swap() 函数吗?

以下片段:#include#includenamespacefoo{templatevoidswap(T&a,T&b){Ttmp=std::move(a);a=std::move(b);b=std::move(tmp);}structbar{};}voidbaz(){std::unique_ptrptr;ptr.reset();}不为我编译:$g++-std=c++11-cfoo.cppInfileincludedfrom/usr/include/c++/5.3.0/memory:81:0,fromfoo.cpp:1:/usr/include/c++/5.3.0/bits/unique

c++ - 可以定义一个完全通用的 swap() 函数吗?

以下片段:#include#includenamespacefoo{templatevoidswap(T&a,T&b){Ttmp=std::move(a);a=std::move(b);b=std::move(tmp);}structbar{};}voidbaz(){std::unique_ptrptr;ptr.reset();}不为我编译:$g++-std=c++11-cfoo.cppInfileincludedfrom/usr/include/c++/5.3.0/memory:81:0,fromfoo.cpp:1:/usr/include/c++/5.3.0/bits/unique

c++ - 等待多个 future ?

我想运行相同类型的任务(工作线程),但一次不超过一定数量的任务。当一个任务完成时,它的结果是一个新任务的输入,然后就可以开始了。有什么好的方法可以在C++11中使用async/future范例来实现这一点吗?乍一看,它看起来很简单,您只需生成多个任务:std::futureresult=std::async(...);然后,运行result.get()以获取任务的异步结果。然而,这里的问题是,future的对象必须存储在某种队列中,并一个一个地等待。不过,可以一遍又一遍地迭代future的对象,检查它们是否准备好了,但由于不必要的CPU负载,这是不希望的。是否有可能以某种方式等待给定集

c++ - 等待多个 future ?

我想运行相同类型的任务(工作线程),但一次不超过一定数量的任务。当一个任务完成时,它的结果是一个新任务的输入,然后就可以开始了。有什么好的方法可以在C++11中使用async/future范例来实现这一点吗?乍一看,它看起来很简单,您只需生成多个任务:std::futureresult=std::async(...);然后,运行result.get()以获取任务的异步结果。然而,这里的问题是,future的对象必须存储在某种队列中,并一个一个地等待。不过,可以一遍又一遍地迭代future的对象,检查它们是否准备好了,但由于不必要的CPU负载,这是不希望的。是否有可能以某种方式等待给定集