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/corotest.cpp

52 lines
1.0 KiB

#include "coro.hpp"
#include <coroutine>
#include <vector>
#include <iostream>
Task<unsigned> task_test()
{
co_await std::suspend_always{};
for (unsigned i = 0; i < 3; ++i)
co_yield i;
co_return 1000;
}
int main()
{
const int task_count = 4;
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;
}
}
}
}
}