1. 19
  1. 7

    I use this technique in an embedded product. It’s really helpful to be able to structure my code in that way; in this case it’s I/O related, and in a microcontroller with no OS to provide threading or anything.

    But it’s a subsystem I only touch once a year or so, and the cognitive load of maintaining it is high, eg. remembering to make sure all your variables are marked static. If there were a way to check those things with the compiler it’d be safer, but… then it wouldn’t be C would it ;-)

    1. 2

      Stack based co-routines. Basically what yield in python does.

      While I find this very neat from one side, I hardly think I would ever pull off something like that on the other. Non-reentrant and frankly quite ugly :-D

      Perhaps in some memory constrained embedded system? But are embedded system memory constrained nowadays?

      Also, wouldn’t it be way more maintainable to pass as a parameter a “generator” function and some opaque pointer to keep that reentrant?

      1. 3

        Yes I prefer to pass a struct rather than use static variables to keep it reentrant. I guess in some situations reentrancy might not be that important though.

        1. 1

          But are embedded system memory constrained nowadays?

          Surprisingly, yes. It’s not uncommon for embedded ARM chips to have memory in the double-digits of kilobytes. Single digits is rarer but not that rare.

        2. 1

          I took a crack at coroutines in C although I had to drop down to assembly to do it (x86 and x86-64). It turned out to be rather simple, although I would only guarantee it for C code (no C++).

          1. 1

            In case the author’s name sounds familiar: https://en.wikipedia.org/wiki/Simon_Tatham

            Stories with similar links:

            1. Coroutines in C (2000) via calvin 7 months ago | 4 points | 3 comments