1
0
mirror of https://github.com/tearshark/librf.git synced 2024-10-02 00:00:11 +08:00
librf/tutorial/test_async_routine.cpp

53 lines
1.5 KiB
C++
Raw Normal View History

2017-09-24 14:01:30 +08:00

#include <chrono>
#include <iostream>
#include <string>
#include <conio.h>
#include <thread>
#include "librf.h"
using namespace resumef;
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
void* frame_ptr = _coro_frame_ptr();
size_t frame_size = _coro_frame_size();
2017-10-01 07:46:41 +08:00
std::cout << "test_routine_use_timer" << std::endl;
2020-02-18 11:42:42 +08:00
std::cout << "frame point=" << frame_ptr << ", size=" << frame_size << ", promise_size=" << _Align_size<promise_t<>>() << std::endl;
2020-02-18 11:32:20 +08:00
auto handler = coroutine_handle<promise_t<>>::from_address(frame_ptr);
auto st = handler.promise().get_state();
2020-02-18 11:32:20 +08:00
scheduler_t* sch = st->get_scheduler();
auto parent = st->get_parent();
std::cout << "st=" << st << ", scheduler=" << sch << ", parent=" << parent << std::endl;
2017-09-24 14:01:30 +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
{
co_await resumef::sleep_for(100ms);
2017-10-01 07:46:41 +08:00
std::cout << "timer after 100ms" << std::endl;
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();
std::cout << "2:frame=" << _coro_frame_ptr() << std::endl;
2017-09-24 14:01:30 +08:00
co_await test_routine_use_timer();
std::cout << "2:frame=" << _coro_frame_ptr() << std::endl;
2017-09-24 14:01:30 +08:00
co_await test_routine_use_timer();
std::cout << "2:frame=" << _coro_frame_ptr() << std::endl;
2017-09-24 14:01:30 +08:00
}
void resumable_main_routine()
{
2020-02-18 11:32:20 +08:00
//go test_routine_use_timer_2();
go test_routine_use_timer();
2017-10-01 10:33:08 +08:00
this_scheduler()->run_until_notask();
2017-09-24 14:01:30 +08:00
}