Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cooperative multi-task / event loop with co_await #199

Open
asfernandes opened this issue May 25, 2021 · 1 comment
Open

Cooperative multi-task / event loop with co_await #199

asfernandes opened this issue May 25, 2021 · 1 comment

Comments

@asfernandes
Copy link

I'd like to create something like an event loop that schedules tasks and do some logic when a task (or sub-task) is co_awaited.

Basically I want something like sync_wait that runs in a loop running just a piece of a task (until its next co_await).

For example this one is a code working, but not given what I want:

#include <cppcoro/task.hpp>
#include <cppcoro/sync_wait.hpp>
#include <iostream>


cppcoro::task<int> f2()
{
	std::cout << "f2.1\n";
	co_return 2;
}

cppcoro::task<int> f1()
{
	std::cout << "f1.1\n";
	auto co = f2();
	std::cout << "f1.2\n";
	auto r = co_await co;
	std::cout << "f1.3\n";
	co_return r;
}


int main()
{
	std::cout << "main.1\n";
	auto co = f1();
	std::cout << "main.2\n";
	auto r = sync_wait(co);
	std::cout << "main.3: " << r << '\n';
	return 0;
}

I want something like this:

int main()
{
	std::cout << "main.1\n";
	auto co = f1();
	std::cout << "main.2\n";

	auto r = start_task(co);
	do
	{
		std::cout << "main loop\n";
	} while (r.continue());

	std::cout << "main.3: " << r << '\n';
	return 0;
}

Can I achieve this with this library?

I need that all the code run in the same thread.

@ddurham2
Copy link

ddurham2 commented Nov 18, 2021

I have the same question. Looking for a way to have coroutinues run atop an event loop. Some events may be ui-event handlers, other events may be the signal to resume a suspended coroutine.

The closest concept I see in the current api is to have a blocking call use an single_consumer_event to suspend itself, and then the resuming event calls .set() to continue it. But how, from the event loop we start a task until it suspends, and then get control back to process the next message, I haven't figured out.

Basically, I'm asking how to get an event handler to invoke a()

... run a() -> b() -> c() ---
 ^                           |
 |                           |
  --------------------------

... and then getting c()'s suspend to return control back to what called a()

Here, maybe c() could co_await a single_consumer_event, but I don't see where control goes at that point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants