4 ms·
the catch: > Because protothreads do not save the stack context across a blocking call, local variables are not preserved when the protothread blocks. This mea
by woodrowbarlow 7y ago
the catch:
> Because protothreads do not save the stack context across a blocking call, local variables are not preserved when the protothread blocks. This means that local variables should be used with utmost care - if in doubt, do not use local variables inside a protothread!
- banachtarski 7y agoThat's literally what "stackless" implies though. I don't know if it's so much of a catch as it is a difference between stackful and stackless routines.
- thethirdone 7y ago> That's literally what "stackless" implies though You could have threads with enough memory for the base levels local variables. It would mean you wouldn't need global variables to keep state, but it would still be stackless as you can't pause execution within a function call.
- alksjdlaksj 7y ago>for the base levels local variables. what does that even mean? Sure you could calculate what stacksize your exact programm would need at maximum, if you unroll everything, but you could not define a fixed "base level" of a stacksize for every single programm out there, could you?
- masklinn 7y ago> what does that even mean? The topmost stack frame. That's what "stackless" generally implies: it doesn't preserve the stack itself, that doesn't mean it preserves nothing. That's what you see with most async/await systems (as opposed to more integrated "lightweight thread" systems like Erlang or Go), yield points blow the stack (though it may be relinked for debugging purposes), but they don't break local variables.
- gpderetta 7y agoNo, not at all. Stackless coroutines only preserve a single call frame, while stackful coroutines [1] preserve the whole stack. For example python, C#, C++ coroutines are stackless, while lua and Go are stackful. It is hardly a coroutine at all if no state, other than the instruction pointer, is preserved. [1] See the lua coroutine papers where the nomenclature is introduced.
- XMPPwocky 7y agoSo if the compiler decides to spill an intermediate value to the stack, you'll have a horribly confusing corruption bug? Fun!
- kccqzy 7y agoNot really. The compiler sees the resulting C code after macro expansion, so the compiler sees the giant switch and the return statements. It won't try to preserve anything.
- 19ylram49 7y agoYes, that’s a significant catch, but this is some good work nonetheless. That said, I’d hope that someone using a C library like this wouldn’t just start using it without understanding the potentially non-trivial consequences/side-effects of that decision.