c – 有效期货与违约构造期货

c – 有效期货与违约构造期货,第1张

概述我正在我的并发编程课中学习期货.我的教授在她的幻灯片中说明了这一点: "Valid" futures are future objects associated to a shared state, and are constructed by calling one of the following functions:asyncpromise::get_futurepackaged_ 我正在我的并发编程课中学习期货.我的教授在她的幻灯片中说明了这一点:
"ValID" futures arefutureobjects associated to ashared state,and are constructed by calling one of the following functions:asyncpromise::get_futurepackaged_task::get_future

futureobjects are only useful when they
arevalID.Default-constructedfutureobjects are
notvalID(unlessmove-assignedavalIDfuture).

我无法理解上述的含义,尤其是“除非移动指定有效的未来”部分.有人可以用简单的术语解释一下,也许还会展示一些示例代码吗?

解决方法 如 std::future constructor所述:

Default-constructed future objects are not valID

这只是意味着调用对象的默认构造函数,如:

std::future<int> f;

这将调用构造函数#1,其中指出:

Default constructor. Constructs a std::future with no shared state.
After construction,valID() == false.

至于其他部分:

(unless move-assigned a valID future)

这里的含义是将调用移动构造函数(future(future&& other)#2),其中指出:

Move constructor. Constructs a std::future with the shared state of
other using move semantics. After construction,other.valID() == false.

基本上,此构造函数中的其他状态将移至此状态.这意味着如果other.valID()== true,那么在移动构造函数返回后,other.valID()将为false,this.valID()将为true.如果other.valID()以false开头,则两者都将以false结尾.

std::future<int> fut; // fut.valID() == false,default constructorstd::future<int> valID_fut = std::async(std::launch::async,[](){ return 42; }); // obtain a valID std::future..// valID_fut.valID() == true here//Now move valID_fut into new_futstd::future<int> new_fut(std::move(valID_fut));// new_fut.valID() == true// valID_fut.valID() == false

总结一下:

>调用std :: future的默认构造函数将导致valID()== false.总是.>调用std :: future的move构造函数只有在other.valID()为true之前才会生成valID()== true.否则就错了.

总结

以上是内存溢出为你收集整理的c – 有效期货与违约构造期货全部内容,希望文章能够帮你解决c – 有效期货与违约构造期货所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1242832.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存