c++ - boost协程如何在coro_t完成后检索函数返回值

标签 c++ boost coroutine

我正在使用 boost::coroutine 调用非 void 函数

typedef boost::coroutines::coroutine< int(void) > coro_t;
typedef coro_t::caller_type Yield_target;

myClass* do_something(Yield_target& yield)
{
    myClass* m = new myClass;
    // ... do smt
    yield();
    // ... do smt
    return m;
}

然后我调用我的函数并将其绑定(bind)到 boost 协程。函数(协程)完成后,如何获取 myClass* 的返回值?

最佳答案

您在问题陈述中提到的类型 coro_t::caller_type 可能已被弃用,因为它未在 more recent Boost Documentation 中列出。 .然而,here在最近的文档中,您可能会在 coroutine<>::push_type 部分看到一个短语,“您无法使用这种协程从另一个执行上下文中检索值”

虽然我不确定您打算如何使用协程库,但我怀疑您会比coroutine<>::pull_type更频繁地使用coroutine<>::push_type>,所以我猜你需要找到另一种方法来检索返回值,而不是库本身。

关于c++ - boost协程如何在coro_t完成后检索函数返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23675272/

相关文章:

c++ - 对于生产者和多个消费者来说,std::queue 线程是安全的

c++ - setjmp 和 longjump 实现线程

c++ - 链接 boost 日志教程时出现链接器错误( undefined reference )

c++ - 模板函数特化,C++

c++ - 使用迭代器将 std::vector 附加到它自己的元素

c++ - 在大数据上使用 boostfiltering_streambuf

c++ - 使用系统命令调用可执行文件

c++ - 如何构建 Notepad++

python - maybeDeferred 模拟与 asyncio

python - 根据 async def 实现协程