#include <unistd.h>
#include <stdio.h>
#include <iostream>
#include <thread>
#include <mutex>
#include <future>
#include <memory>
#include <libgo/coroutine.h>
using namespace std;
const int times = 50;
queue<std::shared_ptr<promise<int>>> q;
queue<int> result;
std::mutex mtx;
void call()
{
for (int i = 0; i < times; i++) {
| mtx.lock();
| std::shared_ptr<std::promise<int>> prom(new promise<int>());
| q.push(prom);
| cout << "call" << to_string(q.size()) << endl;
| mtx.unlock();
| usleep(1);
}
}
void worker()
{
while(true) {
| mtx.lock();
| if (!q.empty()) {
| | cout << "set" << endl;
| | q.front()->set_value(10);
| | result.push(q.front()->get_future().get());
| | q.pop();
| }
| mtx.unlock();
}
}
void poll()
{
cout << result.size();
}
int main()
{
thread t1(&call);
thread t2(&poll);
thread t3(&worker);
t1.join();
t2.join();
t3.join();
return 0;
}