hnes / libaco

A blazing fast and lightweight C asymmetric coroutine library 💎 ⛅🚀⛅🌞
https://libaco.org
Apache License 2.0
3.49k stars 392 forks source link

can libaco support yield/resume between threads? #40

Closed etorth closed 3 years ago

etorth commented 4 years ago

I am using the actor model, actor get scheduled and ran in thread pool.

Means to use a coroutine, it can be yield by any thread of the pool, and then resumed by any thread of the pool. But currently the libaco bind the coroutine to a ``main co" and have to resume by same thread. Is there any possibility to support it?

hnes commented 4 years ago

Hi @etorth :-)

But currently the libaco bind the coroutine to a main co and have to resume by the same thread.

Yes.

Is there any possibility to support it?

Yes again, it is possible to support it from the perspective of technique. But I don't think it is quite necessary, at least in the recent period. But if you do have some very strong reasons, I would be very glad to discuss it with you further.

Here is some advice:

  1. You could just send a msg like (task_fp, task_data_ptr) to the coroutine you want to execute the task no matter these coroutines (i.e. the sender and receiver) are from the same thread or not. Or you could simply treat the coroutine as a service which could receive request msg and reply with the result msg.

  2. Or maybe you could give Golang a try about the thing you wanna do? :-p

zswDev commented 3 years ago

https://github.com/zswDev/libgco/blob/master/task.cpp

change aco->main_co = thread_local main_co

etorth commented 3 years ago

I have switched to c++20's native coroutine support.