c++协程2 (boost::coroutine)

时间:2021-06-11 01:02:22
#include <functional>
#include <iostream>
#include <boost/coroutine/all.hpp>


using boost::coroutines::coroutine;

void cooperative(coroutine<int>::push_type &sink, int i)
{
	int j = i;

	//调用main
	sink(++j);

	//调用main
	sink(++j);

	std::cout << "end\n";
}

int main()
{
	using std::placeholders::_1;

	//传入一个参数,初始值为0
	coroutine<int>::pull_type source{ std::bind(cooperative, _1, 0) };
	std::cout << source.get() << '\n';

	//调用cooperative
	source();
	std::cout << source.get() << '\n';

	//调用cooperative
	source();
}

c++协程2 (boost::coroutine)