2017-09-24 14:01:30 +08:00
|
|
|
|
|
|
|
|
|
#include <chrono>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <conio.h>
|
|
|
|
|
#include <thread>
|
|
|
|
|
#include <deque>
|
|
|
|
|
#include <mutex>
|
|
|
|
|
|
|
|
|
|
#include "librf.h"
|
|
|
|
|
|
|
|
|
|
using namespace resumef;
|
|
|
|
|
|
2017-11-27 18:37:18 +08:00
|
|
|
|
const size_t MAX_CHANNEL_QUEUE = 5; //0, 1, 5, 10, -1
|
2017-09-24 14:01:30 +08:00
|
|
|
|
|
2017-11-27 18:37:18 +08:00
|
|
|
|
future_vt test_channel_read(const channel_t<std::string> & c)
|
2017-09-24 14:01:30 +08:00
|
|
|
|
{
|
|
|
|
|
using namespace std::chrono;
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < 10; ++i)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2017-11-27 18:37:18 +08:00
|
|
|
|
auto val = co_await c.read();
|
|
|
|
|
//auto val = co_await c; //第二种从channel读出数据的方法。利用重载operator co_await(),而不是c是一个awaitable_t。
|
2017-09-24 14:01:30 +08:00
|
|
|
|
|
|
|
|
|
std::cout << val << ":";
|
|
|
|
|
#if _DEBUG
|
|
|
|
|
for (auto val : c.debug_queue())
|
|
|
|
|
std::cout << val << ",";
|
|
|
|
|
#endif
|
|
|
|
|
std::cout << std::endl;
|
|
|
|
|
}
|
|
|
|
|
catch (channel_exception e)
|
|
|
|
|
{
|
|
|
|
|
//MAX_CHANNEL_QUEUE=0,并且先读后写,会触发read_before_write异常
|
|
|
|
|
std::cout << e.what() << std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
co_await sleep_for(50ms);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-27 18:37:18 +08:00
|
|
|
|
future_vt test_channel_write(const channel_t<std::string> & c)
|
2017-09-24 14:01:30 +08:00
|
|
|
|
{
|
|
|
|
|
using namespace std::chrono;
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < 10; ++i)
|
|
|
|
|
{
|
2017-11-27 18:37:18 +08:00
|
|
|
|
co_await c.write(std::to_string(i));
|
|
|
|
|
//co_await (c << std::to_string(i)); //第二种写入数据到channel的方法。因为优先级关系,需要将'c << i'括起来
|
2017-09-24 14:01:30 +08:00
|
|
|
|
std::cout << "<" << i << ">:";
|
|
|
|
|
|
|
|
|
|
#if _DEBUG
|
|
|
|
|
for (auto val : c.debug_queue())
|
|
|
|
|
std::cout << val << ",";
|
|
|
|
|
#endif
|
|
|
|
|
std::cout << std::endl;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void test_channel_read_first()
|
|
|
|
|
{
|
2017-11-27 18:37:18 +08:00
|
|
|
|
channel_t<std::string> c(MAX_CHANNEL_QUEUE);
|
2017-09-24 14:01:30 +08:00
|
|
|
|
|
|
|
|
|
go test_channel_read(c);
|
|
|
|
|
go test_channel_write(c);
|
|
|
|
|
|
2017-10-01 10:33:08 +08:00
|
|
|
|
this_scheduler()->run_until_notask();
|
2017-09-24 14:01:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void test_channel_write_first()
|
|
|
|
|
{
|
2017-11-27 18:37:18 +08:00
|
|
|
|
channel_t<std::string> c(MAX_CHANNEL_QUEUE);
|
2017-09-24 14:01:30 +08:00
|
|
|
|
|
|
|
|
|
go test_channel_write(c);
|
|
|
|
|
go test_channel_read(c);
|
|
|
|
|
|
2017-10-01 10:33:08 +08:00
|
|
|
|
this_scheduler()->run_until_notask();
|
2017-09-24 14:01:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void resumable_main_channel()
|
|
|
|
|
{
|
|
|
|
|
test_channel_read_first();
|
|
|
|
|
std::cout << std::endl;
|
|
|
|
|
|
|
|
|
|
test_channel_write_first();
|
|
|
|
|
}
|