簡體   English   中英

提高協程斷言失敗

[英]Boost coroutine assertion failure

為了試用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;        
    }
}

該代碼基於docs中示例代碼

我的構建命令如下:

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

輸出:

$ ./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)

由於斷言失敗,程序被中止。 您能幫我在代碼中找到錯誤嗎?

我相信您需要在函數f的開頭添加一個調用ca()

從boost文檔中:

執行控制在構造時轉移到協程(輸入了協程功能)-當應將控制權返回到原始調用例程時,在boost類型的第一個參數上調用boost :: coroutines :: coroutine <> :: operator(): coroutine函數中的:coroutines :: coroutine <> :: caller_type。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM