void helloFiber(boost::fibers::future<void> &f)
{
cout << "Hello, boost::fiber" << endl;
f.get();
}
int main()
{
boost::fibers::promise<void> pm;
boost::fibers::future<void> ft = pm.get_future();
{
boost::fibers::fiber f(helloFiber, std::move(ft));
cout << "Before join." << endl;
f.detach();
}
pm.set_value();
cout << "After join." << endl;
return 0;
}此程序输出:加入前。加入之后。你好,加油::纤维。
为什么它不输出:在加入之前。你好,加油::纤维后加入。
发布于 2016-10-28 11:44:17
您应该将helloFiber()的签名更改为对未来的rvalue引用(移动未来)。
因为您分离了光纤,所以调度程序必须加入它(在您的示例中)。
请查看:mgmt.html (部分:枚举启动):
枚举启动指定控件是否立即传递到新启动的光纤中。
boost::fibers::fiber f( boost::fibers::launch::post, helloFiber, std::move(ft));
boost::fibers::fiber f( boost::fibers::launch::dispatch, helloFiber, std::move(ft));耳聋是post -但是您想要调度,所以输出是:
你好,加油::加入前纤维。加入之后。
它永远不会打印:“加入前。你好,增强::纤维在连接后。”因为你把
cout << "Before join." << endl;之后
boost::fibers::fiber f(helloFiber, std::move(ft));https://stackoverflow.com/questions/40289626
复制相似问题