3 ms·
Event loops lose your call stack; you need to rewrite your program as a state machine so you know where to pick back up; or you need to rewrite your program in
by barrkel 11y ago
Event loops lose your call stack; you need to rewrite your program as a state machine so you know where to pick back up; or you need to rewrite your program in continuation passing style with callbacks, like JavaScript; or you automate that CPS transform, and you've just reinvented green threads again.
- Matthias247 11y agoI agree with what you (and the others) say. Regarding green threads and state machines I would however add that while green threads allow to ditch some state machines (e.g. ones that wait until a complete packet was received) you won't get rid of all of them, e.g. you will still need state machines for lots of protocol implementations. E.g. if you are implementing the websocket protocol or HTTP/2, you have some interaction between incoming data, data that must be sent out and requests from the [library] user. The state of the connection depends on all of these, so you need some kind of connection state machine which would run most likely in it's own green thread (in addition to a sending thread and a receiving thread and a user thread). And now you have to implement clean synchronization between all of those threads. The lack of necessary synchronization (and the ability to observe a huge amount of state at once without danger) can make the implementation a little bit more easy in my experience. But here I also have to agree with strmpnk that a good design is necessary for both approaches, and accessing lots of shared mutable data in multiple callbacks is probably also not the cleanest approach.
- zzzcpan 11y agoThere is a very big difference between implicit flow control, that you have with green threads and explicit one, that you have with "reinvented green threads", as you call them. First one will force you to think about synchronization to access your memory, but the second one - won't. It will also let you do clean cancellation, etc., that you won't be able to do with green thread.