2017-11-27 18:37:18 +08:00
|
|
|
|
//验证channel是否线程安全
|
|
|
|
|
|
|
|
|
|
#include <chrono>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <conio.h>
|
|
|
|
|
#include <thread>
|
|
|
|
|
#include <deque>
|
|
|
|
|
#include <mutex>
|
|
|
|
|
|
|
|
|
|
#include "librf.h"
|
|
|
|
|
|
|
|
|
|
using namespace resumef;
|
|
|
|
|
|
|
|
|
|
using namespace std::chrono;
|
|
|
|
|
static std::mutex cout_mutex;
|
2017-12-14 10:50:45 +08:00
|
|
|
|
std::atomic<intptr_t> gcounter = 0;
|
2017-11-27 18:37:18 +08:00
|
|
|
|
|
|
|
|
|
#define OUTPUT_DEBUG 0
|
|
|
|
|
|
|
|
|
|
future_vt test_channel_consumer(const channel_t<std::string> & c, size_t cnt)
|
|
|
|
|
{
|
|
|
|
|
for (size_t i = 0; i < cnt; ++i)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
auto val = co_await c.read();
|
2017-12-14 10:50:45 +08:00
|
|
|
|
++gcounter;
|
2017-11-27 18:37:18 +08:00
|
|
|
|
#if OUTPUT_DEBUG
|
|
|
|
|
{
|
|
|
|
|
scoped_lock<std::mutex> __lock(cout_mutex);
|
|
|
|
|
std::cout << "R " << val << "@" << std::this_thread::get_id() << std::endl;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
catch (channel_exception e)
|
|
|
|
|
{
|
|
|
|
|
//MAX_CHANNEL_QUEUE=0,并且先读后写,会触发read_before_write异常
|
|
|
|
|
scoped_lock<std::mutex> __lock(cout_mutex);
|
|
|
|
|
std::cout << e.what() << std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if OUTPUT_DEBUG
|
|
|
|
|
co_await sleep_for(50ms);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
future_vt test_channel_producer(const channel_t<std::string> & c, size_t cnt)
|
|
|
|
|
{
|
|
|
|
|
for (size_t i = 0; i < cnt; ++i)
|
|
|
|
|
{
|
|
|
|
|
co_await c.write(std::to_string(i));
|
|
|
|
|
#if OUTPUT_DEBUG
|
|
|
|
|
{
|
|
|
|
|
scoped_lock<std::mutex> __lock(cout_mutex);
|
|
|
|
|
std::cout << "W " << i << "@" << std::this_thread::get_id() << std::endl;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const size_t N = 8;
|
|
|
|
|
const size_t BATCH = 1000000;
|
|
|
|
|
const size_t MAX_CHANNEL_QUEUE = N + 1; //0, 1, 5, 10, -1
|
|
|
|
|
|
|
|
|
|
void resumable_main_channel_mult_thread()
|
|
|
|
|
{
|
|
|
|
|
channel_t<std::string> c(MAX_CHANNEL_QUEUE);
|
|
|
|
|
|
2017-12-13 17:49:38 +08:00
|
|
|
|
std::thread write_th([&]
|
2017-11-27 18:37:18 +08:00
|
|
|
|
{
|
2017-12-14 10:50:45 +08:00
|
|
|
|
local_scheduler my_scheduler; //2017/12/14日,仍然存在BUG。真多线程下调度,存在有协程无法被调度完成的BUG
|
2017-11-27 18:37:18 +08:00
|
|
|
|
go test_channel_producer(c, BATCH * N);
|
2017-12-14 10:50:45 +08:00
|
|
|
|
#if RESUMEF_ENABLE_MULT_SCHEDULER
|
2017-11-27 18:37:18 +08:00
|
|
|
|
this_scheduler()->run_until_notask();
|
2017-12-14 10:50:45 +08:00
|
|
|
|
#endif
|
2017-12-13 17:49:38 +08:00
|
|
|
|
std::cout << "Write OK\r\n";
|
2017-11-27 18:37:18 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
//std::this_thread::sleep_for(100ms);
|
|
|
|
|
|
2017-12-13 17:49:38 +08:00
|
|
|
|
std::thread read_th[N];
|
2017-11-27 18:37:18 +08:00
|
|
|
|
for (size_t i = 0; i < N; ++i)
|
|
|
|
|
{
|
2017-12-13 17:49:38 +08:00
|
|
|
|
read_th[i] = std::thread([&]
|
2017-11-27 18:37:18 +08:00
|
|
|
|
{
|
2017-12-14 10:50:45 +08:00
|
|
|
|
local_scheduler my_scheduler; //2017/12/14日,仍然存在BUG。真多线程下调度,存在有协程无法被调度完成的BUG
|
2017-11-27 18:37:18 +08:00
|
|
|
|
go test_channel_consumer(c, BATCH);
|
2017-12-14 10:50:45 +08:00
|
|
|
|
#if RESUMEF_ENABLE_MULT_SCHEDULER
|
2017-11-27 18:37:18 +08:00
|
|
|
|
this_scheduler()->run_until_notask();
|
2017-12-14 10:50:45 +08:00
|
|
|
|
#endif
|
2017-12-13 17:49:38 +08:00
|
|
|
|
std::cout << "Read OK\r\n";
|
2017-11-27 18:37:18 +08:00
|
|
|
|
});
|
|
|
|
|
}
|
2017-12-14 10:50:45 +08:00
|
|
|
|
|
|
|
|
|
#if !RESUMEF_ENABLE_MULT_SCHEDULER
|
|
|
|
|
std::this_thread::sleep_for(100ms);
|
|
|
|
|
scheduler::g_scheduler.run_until_notask();
|
|
|
|
|
#endif
|
2017-12-13 17:49:38 +08:00
|
|
|
|
for(auto & th : read_th)
|
2017-11-27 18:37:18 +08:00
|
|
|
|
th.join();
|
2017-12-13 17:49:38 +08:00
|
|
|
|
write_th.join();
|
2017-11-27 18:37:18 +08:00
|
|
|
|
|
2017-12-14 10:50:45 +08:00
|
|
|
|
std::cout << "OK: counter = " << gcounter.load() << std::endl;
|
2018-08-26 23:46:02 +08:00
|
|
|
|
(void)_getch();
|
2017-11-27 18:37:18 +08:00
|
|
|
|
}
|