boost 对称协程symmetric_coroutine

梦想游戏人
目录:
现代C++

对称协程 symmetric_coroutine 

非对称协程 asymmetric_coroutine

  • #include "boost_1_60_0/boost/coroutine/coroutine.hpp"
  • int main()
  • {
  • using Coroutine_t = boost::coroutines::symmetric_coroutine<int>;
  • Coroutine_t::call_type coro_recv(
  • [&](Coroutine_t::yield_type& yield) {
  • //loop for recv
  • for (;;)
  • {
  • std::cout << yield.get() << " recv" << endl;
  • yield(); //give up time slice and jump back to starting context
  • }
  • });
  • Coroutine_t::call_type coro_send(
  • [&](Coroutine_t::yield_type& yield) {
  • //loop for send
  • for (;;)
  • {
  • yield.get();
  • std::cout << yield.get() << " send" << endl;
  • yield();
  • }
  • });
  • while (true)
  • {
  • coro_recv(1); //recv
  • coro_send(1); // send
  • }
  • system("pause");
  • return 0;
  • }
#include "boost_1_60_0/boost/coroutine/coroutine.hpp"

int main()
{
	using   Coroutine_t = boost::coroutines::symmetric_coroutine<int>;

	Coroutine_t::call_type coro_recv(
		[&](Coroutine_t::yield_type& yield) {
		//loop for recv
		for (;;)
		{
			std::cout << yield.get() << " recv" << endl;
			yield(); //give up time  slice and jump back to starting context
		}
	});

	Coroutine_t::call_type coro_send(
		[&](Coroutine_t::yield_type& yield) {
		//loop for send
		for (;;)
		{
			yield.get();
			std::cout << yield.get() << " send" << endl;
			yield(); 
		}
	});


	while (true)
	{
		coro_recv(1); //recv

		coro_send(1); // send

	}

	system("pause");

	return 0;
}

此外可以用ASIO 的 定时器 和协程 来完成 定时任务

  • #include "boost_1_60_0/boost/asio/steady_timer.hpp"
  • #include "boost_1_60_0/boost/asio/spawn.hpp"
  • int main()
  • {
  • boost::asio::io_service io;
  • boost::asio::steady_timer timer(io);
  • using namespace boost::asio;
  • //协程1
  • spawn(io, [&](yield_context yield) {
  • while (true)
  • {
  • boost::asio::steady_timer timer(io);
  • timer.expires_from_now(std::chrono::seconds(1));
  • timer.async_wait(yield);
  • cout << "1111111111" << endl;;
  • }
  • });
  • //协程2
  • spawn(io, [&](yield_context yield) {
  • while (true)
  • {
  • boost::asio::steady_timer timer(io);
  • timer.expires_from_now(std::chrono::milliseconds(500));
  • timer.async_wait(yield);
  • cout << "22222222222"<<endl;
  • }
  • });
  • io.run();//
  • while (true)
  • {
  • Sleep(100);
  • }
  • system("pause");
  • return 0;
  • }
#include "boost_1_60_0/boost/asio/steady_timer.hpp"
#include "boost_1_60_0/boost/asio/spawn.hpp"
int main()
{
	boost::asio::io_service io;

	boost::asio::steady_timer timer(io);
	using namespace boost::asio;


	//协程1
	spawn(io, [&](yield_context yield) {
		while (true)
		{
			boost::asio::steady_timer timer(io);
			timer.expires_from_now(std::chrono::seconds(1));
			timer.async_wait(yield);
	 
			cout << "1111111111" << endl;;
		}
	});

	//协程2
	spawn(io, [&](yield_context yield) {
		while (true)
		{
			boost::asio::steady_timer timer(io);
			timer.expires_from_now(std::chrono::milliseconds(500));
			timer.async_wait(yield);
		
			cout << "22222222222"<<endl;
		}
	});
		

	io.run();//



	while (true)
	{
		Sleep(100);
	}


	system("pause");

	return 0;
}
Scroll Up