How to implement a practical fiber scheduler?

前端 未结 5 1256
独厮守ぢ
独厮守ぢ 2020-12-30 09:29

I know the very basics about using coroutines as a base and implementing a toy scheduler. But I assume it\'s oversimplified view about asynchronous schedulers in whole. Ther

5条回答
  •  渐次进展
    2020-12-30 10:30

    You'd need to multiplex io operations into an event based interface(select/poll), so you can leverage the OS to do the waiting, while still being able to schedule other fibers. select/poll have a timeout argument - for fibers that want to sleep, you can create a priority queue that uses that option of select/poll to emulate a sleep call.

    Trying to serve fibers that does blocking operations (call read/write/sleep etc). directly won't work unless you schedule each fiber in a native thread - which kind of beats the purpose.

    See http://swtch.com/libtask/ for a working implementation.

提交回复
热议问题