1
0
mirror of https://github.com/tearshark/librf.git synced 2024-10-02 00:00:11 +08:00
librf/tutorial/test_async_mutex.cpp
tearshark 84fe5c4b1a 测试channel在多线程多协程调度下的表现
测试结果表明N:M模型的代码还存在BUG
2017-11-27 18:37:18 +08:00

55 lines
896 B
C++

#include <chrono>
#include <iostream>
#include <string>
#include <conio.h>
#include <thread>
#include <deque>
#include "librf.h"
using namespace resumef;
mutex_t g_lock;
std::deque<size_t> g_queue;
future_vt test_mutex_pop(size_t idx)
{
using namespace std::chrono;
for (size_t i = 0; i < 10; ++i)
{
co_await resumf_guard_lock(g_lock);
if (g_queue.size() > 0)
{
size_t val = g_queue.front();
g_queue.pop_front();
std::cout << val << " on " << idx << std::endl;
}
co_await sleep_for(500ms);
}
}
future_vt test_mutex_push()
{
using namespace std::chrono;
for (size_t i = 0; i < 10; ++i)
{
co_await resumf_guard_lock(g_lock);
g_queue.push_back(i);
co_await sleep_for(500ms);
}
}
void resumable_main_mutex()
{
go test_mutex_push();
go test_mutex_pop(1);
this_scheduler()->run_until_notask();
}