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

Few questions about proper usage #60

Open zukonake opened 1 month ago

zukonake commented 1 month ago

I'm planning to use libaco in C++, but I need some clarifications about those 3 scenarios, whether they are not UB and will work correctly.

  1. Creating a coroutine with a non main coroutine as the "main"/parent parameter. So when I use aco_create to create non-main co A, and then pass the resulting coroutine into another aco_create call to create B, as the main co parameter, it seems to work fine. i.e. B yields to A, then A can yield to main. Is this valid though?
  2. Can there be 2 main coroutines in a single thread?
  3. Can aco_exit() be called from a C++ destructor? So I've noticed that aco_exit() causes C++ destructors in a coroutine NOT to fire, which is a problem. As a solution I found out that creating a dummy object at the beginning of the function, which calls aco_exit() as its dtor, actually lets other destructors to fire before aco_exit, and it works even with a return statement, without invoking the "last word" crash.