higan-emu / libco

libco is a cooperative multithreading library written in C89.
Other
128 stars 24 forks source link

What's yield in libco? #9

Closed lygstate closed 4 years ago

namandixit commented 4 years ago

First, you would find the handle of the coroutine to which you want to yield to using co_active(), and then would jump ("yield") to it from another coroutine using co_switch().

For more details, look up Symmetric Coroutines vs Asymmetric Coroutines, and how one can be emulated in terms of another. Here's one paper from Lua's developers.

lygstate commented 4 years ago

and also, once a co_thread are finished, what's I need to do to remove it from co_switch? I need some demo for that. This is a wonderfull library that also works on bare-metal:) Just need a bit improvement about the API to easier to use

Screwtapello commented 4 years ago

co_switch() doesn't automatically switch to another thread, it only switches to the thread you name. If you want to stop switching to a particular thread, you just stop calling co_switch() on it.

If you never want to switch to a particular thread again, you should stop calling co_switch() on it, and probably also call co_delete() on it to deallocate the memory used for the stack.