1
0
鏡像自 https://github.com/tearshark/librf.git synced 2024-10-04 08:50:31 +08:00
librf/tutorial/test_async_routine.cpp

52 行
1.2 KiB
C++

2021-11-01 17:59:08 +08:00

2017-09-24 14:01:30 +08:00
#include <chrono>
#include <iostream>
#include <string>
#include <thread>
2021-11-01 17:59:08 +08:00
#include "librf/librf.h"
2017-09-24 14:01:30 +08:00
2021-11-01 17:59:08 +08:00
using namespace librf;
2017-09-24 14:01:30 +08:00
2021-11-01 17:59:08 +08:00
#ifndef __GNUC__ //GCC: 没有提供__builtin_coro_frame这样的内置函数
future_t<> test_routine_use_timer()
2017-09-24 14:01:30 +08:00
{
using namespace std::chrono;
2020-02-18 11:32:20 +08:00
2017-10-01 07:46:41 +08:00
for (size_t i = 0; i < 3; ++i)
2017-09-24 14:01:30 +08:00
{
2021-11-01 17:59:08 +08:00
co_await librf::sleep_for(100ms);
2017-10-01 07:46:41 +08:00
std::cout << "timer after 100ms" << std::endl;
2020-04-18 13:17:02 +08:00
std::cout << "1:frame=" << _coro_frame_ptr() << std::endl;
2017-09-24 14:01:30 +08:00
}
}
future_t<> test_routine_use_timer_2()
2017-09-24 14:01:30 +08:00
{
2017-10-01 07:46:41 +08:00
std::cout << "test_routine_use_timer_2" << std::endl;
2017-09-24 14:01:30 +08:00
co_await test_routine_use_timer();
2020-04-18 13:17:02 +08:00
std::cout << "2:frame=" << _coro_frame_ptr() << std::endl;
2017-09-24 14:01:30 +08:00
co_await test_routine_use_timer();
2020-04-18 13:17:02 +08:00
std::cout << "2:frame=" << _coro_frame_ptr() << std::endl;
2017-09-24 14:01:30 +08:00
co_await test_routine_use_timer();
2020-04-18 13:17:02 +08:00
std::cout << "2:frame=" << _coro_frame_ptr() << std::endl;
2017-09-24 14:01:30 +08:00
}
2020-04-18 13:17:02 +08:00
#endif //#ifndef __GNUC__
2017-09-24 14:01:30 +08:00
void resumable_main_routine()
{
std::cout << __FUNCTION__ << std::endl;
2020-02-18 11:32:20 +08:00
//go test_routine_use_timer_2();
2021-11-01 17:59:08 +08:00
#ifndef __GNUC__ //GCC: 没有提供__builtin_coro_frame这样的内置函数
2020-02-18 11:32:20 +08:00
go test_routine_use_timer();
2020-04-18 13:17:02 +08:00
#endif //#ifndef __GNUC__
2017-10-01 10:33:08 +08:00
this_scheduler()->run_until_notask();
2017-09-24 14:01:30 +08:00
}
2020-09-23 22:56:51 +08:00
int main()
{
resumable_main_routine();
return 0;
}