简体   繁体   English

提高协程断言失败

[英]Boost coroutine assertion failure

To try out the new coroutine feature in boost I created the following program: 为了试用boost中的新协程功能,我创建了以下程序:

#include <boost/coroutine/all.hpp>
#include <string>
#include <vector>


typedef boost::coroutines::coroutine<int(char)> coroutine_t;


void f(coroutine_t::caller_type & ca)
{
    std::vector<int> vec = {1, 2, 3};
    for (int i : vec)
    {
        char c = ca.get();
        std::cout << "c: " << c << std::endl;
        ca(i);
    }
}

int main()
{
    coroutine_t cr(f);
    std::string str("abc");
    for (char c : str)
    {
        std::cout << c << std::flush;
        cr(c);
        int n = cr.get();
        std::cout << n << std::endl;        
    }
}

The code is based on the sample code from the docs . 该代码基于docs中示例代码

My build command goes as follows: 我的构建命令如下:

$ g++ -std=c++11 -o test -I/usr/local/include -L/usr/local/lib main.cpp /usr/local/lib/libboost_context.a

Output: 输出:

$ ./test
test: /usr/local/include/boost/coroutine/detail/coroutine_get.hpp:43: typename boost::coroutines::detail::param<Result>::type boost::coroutines::detail::coroutine_get<D, Result, arity>::get() const [with D = boost::coroutines::coroutine<char(int), 1>; Result = char; int arity = 1; typename boost::coroutines::detail::param<Result>::type = char]: Assertion `static_cast< D const* >( this)->impl_->result_' failed.
Aborted (core dumped)

The program is aborted due to failed assertion. 由于断言失败,程序被中止。 Can you help me find the error in my code? 您能帮我在代码中找到错误吗?

I believe you need to add a call ca() at the beginning of your function f . 我相信您需要在函数f的开头添加一个调用ca()

From the boost documentation: 从boost文档中:

The execution control is transferred to coroutine at construction (coroutine-function entered) - when control should be returned to the original calling routine, invoke boost::coroutines::coroutine<>::operator() on the first argument of type boost::coroutines::coroutine<>::caller_type inside coroutine-function. 执行控制在构造时转移到协程(输入了协程功能)-当应将控制权返回到原始调用例程时,在boost类型的第一个参数上调用boost :: coroutines :: coroutine <> :: operator(): coroutine函数中的:coroutines :: coroutine <> :: caller_type。

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

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