Sta*_*ked 6 c++ boost boost-coroutine
为了在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;
}
}
Run Code Online (Sandbox Code Playgroud)
我的构建命令如下:
$ g++ -std=c++11 -o test -I/usr/local/include -L/usr/local/lib main.cpp /usr/local/lib/libboost_context.a
Run Code Online (Sandbox Code Playgroud)
输出:
$ ./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)
Run Code Online (Sandbox Code Playgroud)
程序因断言失败而中止.你能帮我找到代码中的错误吗?
我相信您需要ca()在函数的开头添加一个调用f。
来自提升文档:
执行控制在构造时转移到协程(输入协程函数) - 当控制权应返回到原始调用例程时,对 boost 类型的第一个参数调用 boost::coroutines::coroutine<>::operator() : :coroutines::coroutine<>::caller_type 位于协程函数内。
| 归档时间: |
|
| 查看次数: |
639 次 |
| 最近记录: |