Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boost coroutine assertion failure

To try out the new coroutine feature in boost I created the following program:

#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.

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?

like image 313
StackedCrooked Avatar asked Feb 05 '13 12:02

StackedCrooked


1 Answers

I believe you need to add a call ca() at the beginning of your function f.

From the boost documentation:

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.

like image 114
jmetcalfe Avatar answered Nov 04 '22 05:11

jmetcalfe