A weird game.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
turings-tarpit/scratchpad/corotest.cpp

53 lines
1.0 KiB

#include "../coro.hpp"
#include <coroutine>
#include <vector>
#include <iostream>
using std::cout, std::endl;
Task<unsigned> task_test()
{
co_await Pass{};
for (unsigned i = 0; i < 3; ++i)
co_yield i;
co_return 1000;
}
int main()
{
const int task_count = 4;
std::vector<Task<unsigned>> tasks;
for(int i = 0; i < task_count; i++) {
auto t = task_test();
tasks.push_back(std::move(t));
}
int done_count = 0;
while(done_count < task_count) {
for(int i = 0; i < task_count; i++) {
Task<unsigned> &t = tasks[i];
if(t.done()) {
// remove it from the tasks
// this cause crash I think?
t.destroy();
done_count++;
} else {
auto res = t();
if(t.state() == AWAIT) {
cout << "AWAIT! " << t.state() << endl;
} else if(t.state() != YIELD) {
cout << "NOT YIELD: " << t.state() << endl;
} else {
cout << "T# " << i << " result "
<< res << " STATE " << t.state() << endl;
}
}
}
}
}