简体   繁体   English

通过函数参数传递boost.coroutine

[英]Passing boost.coroutine through function arguments

Well, my problem is to properly pass boost::coroutines::coroutine<std::string(void)> object as function argument. 好吧,我的问题是正确地将boost::coroutines::coroutine<std::string(void)>对象作为函数参数传递。 I can't do that by reference because calling this method is realized by boost::bind and boost::asio (there is a possibility that this memory address will be invalid). 我无法通过引用做到这一点,因为调用此方法是通过boost::bindboost::asio (此内存地址可能无效)。 So, is there any easy way to do that? 那么,有没有简单的方法可以做到这一点? Or the only way which left me is to pack this object into pointer? 还是让我离开的唯一方法是将该对象打包到指针中?

you could let a shared_ptr manage the coroutine and pass a weak_ptr to bind(). 您可以让shared_ptr管理协程,然后将weak_ptr传递给bind()。 In the callback-function you could check via weak_ptr if the shared_ptr is still valid: 在回调函数中,您可以通过weak_ptr检查shared_ptr是否仍然有效:

shared_ptr< coroutine<...> > sp = wp.lock();
if (sp) {
...
} else {
// shared_ptr was released
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM