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

193 lines
3.8 KiB
C++
Raw Normal View History

2020-03-26 17:35:12 +08:00
#include <chrono>
2017-09-24 14:01:30 +08:00
#include <iostream>
#include <string>
#include <thread>
#include "librf.h"
using namespace resumef;
2019-10-09 09:43:10 +08:00
//非协程的逻辑线程或异步代码可以通过event_t通知到协程并且不会阻塞协程所在的线程。
2020-03-10 21:31:05 +08:00
static std::thread async_set_event(const event_t & e, std::chrono::milliseconds dt)
2017-09-24 14:01:30 +08:00
{
return std::thread([=]
{
std::this_thread::sleep_for(dt);
e.signal();
});
}
2020-03-10 21:31:05 +08:00
static future_t<> resumable_wait_event(const event_t & e)
2017-09-24 14:01:30 +08:00
{
using namespace std::chrono;
if (co_await e.wait() == false)
std::cout << "time out!" << std::endl;
else
std::cout << "event signal!" << std::endl;
}
2020-03-10 21:31:05 +08:00
static void test_wait_one()
2017-09-24 14:01:30 +08:00
{
using namespace std::chrono;
{
event_t evt;
go resumable_wait_event(evt);
auto tt = async_set_event(evt, 1000ms);
2017-10-01 10:33:08 +08:00
this_scheduler()->run_until_notask();
2017-09-24 14:01:30 +08:00
tt.join();
}
{
event_t evt2(1);
go[&]() -> future_t<>
2017-09-24 14:01:30 +08:00
{
(void)co_await evt2.wait();
std::cout << "event signal on 1!" << std::endl;
};
go[&]() -> future_t<>
2017-09-24 14:01:30 +08:00
{
(void)co_await evt2.wait();
std::cout << "event signal on 2!" << std::endl;
};
std::cout << std::this_thread::get_id() << std::endl;
auto tt = async_set_event(evt2, 1000ms);
2017-10-01 10:33:08 +08:00
this_scheduler()->run_until_notask();
2017-09-24 14:01:30 +08:00
tt.join();
}
}
static void test_wait_three()
{
using namespace std::chrono;
event_t evt1, evt2, evt3;
go[&]() -> future_t<>
{
2020-03-31 17:37:13 +08:00
if (co_await event_t::wait_all(std::initializer_list<event_t>{ evt1, evt2, evt3 }))
std::cout << "all event signal!" << std::endl;
else
std::cout << "time out!" << std::endl;
};
std::vector<std::thread> vtt;
srand((int)time(nullptr));
vtt.emplace_back(async_set_event(evt1, 1ms * (500 + rand() % 1000)));
vtt.emplace_back(async_set_event(evt2, 1ms * (500 + rand() % 1000)));
vtt.emplace_back(async_set_event(evt3, 1ms * (500 + rand() % 1000)));
this_scheduler()->run_until_notask();
for (auto& tt : vtt)
tt.join();
}
2020-03-10 21:31:05 +08:00
static void test_wait_any()
2017-09-24 14:01:30 +08:00
{
using namespace std::chrono;
event_t evts[8];
go[&]() -> future_t<>
2017-09-24 14:01:30 +08:00
{
for (size_t i = 0; i < std::size(evts); ++i)
2017-09-24 14:01:30 +08:00
{
intptr_t idx = co_await event_t::wait_any(evts);
std::cout << "event " << idx << " signal!" << std::endl;
}
};
std::vector<std::thread> vtt;
srand((int)time(nullptr));
for (auto & e : evts)
{
vtt.emplace_back(async_set_event(e, 1ms * (500 + rand() % 1000)));
}
2017-10-01 10:33:08 +08:00
this_scheduler()->run_until_notask();
2017-09-24 14:01:30 +08:00
for (auto & tt : vtt)
tt.join();
}
2020-03-10 21:31:05 +08:00
static void test_wait_all()
2017-09-24 14:01:30 +08:00
{
using namespace std::chrono;
event_t evts[8];
go[&]() -> future_t<>
2017-09-24 14:01:30 +08:00
{
if (co_await event_t::wait_all(evts))
std::cout << "all event signal!" << std::endl;
else
std::cout << "time out!" << std::endl;
};
std::vector<std::thread> vtt;
srand((int)time(nullptr));
for (auto & e : evts)
{
vtt.emplace_back(async_set_event(e, 1ms * (500 + rand() % 1000)));
}
2017-10-01 10:33:08 +08:00
this_scheduler()->run_until_notask();
2017-09-24 14:01:30 +08:00
for (auto & tt : vtt)
tt.join();
}
2020-03-10 21:31:05 +08:00
static void test_wait_all_timeout()
2017-09-24 14:01:30 +08:00
{
using namespace std::chrono;
event_t evts[8];
go[&]() -> future_t<>
2017-09-24 14:01:30 +08:00
{
if (co_await event_t::wait_all_for(1000ms, evts))
std::cout << "all event signal!" << std::endl;
else
std::cout << "time out!" << std::endl;
};
std::vector<std::thread> vtt;
srand((int)time(nullptr));
for (auto & e : evts)
{
vtt.emplace_back(async_set_event(e, 1ms * (500 + rand() % 1000)));
}
2017-10-01 10:33:08 +08:00
this_scheduler()->run_until_notask();
2017-09-24 14:01:30 +08:00
for (auto & tt : vtt)
tt.join();
}
void resumable_main_event()
{
2020-03-17 18:26:22 +08:00
test_wait_one();
std::cout << std::endl;
2017-09-24 14:01:30 +08:00
test_wait_three();
std::cout << std::endl;
2017-09-24 14:01:30 +08:00
test_wait_any();
std::cout << std::endl;
test_wait_all();
std::cout << std::endl;
test_wait_all_timeout();
std::cout << std::endl;
}