State machines in C

前端 未结 9 1737
一整个雨季
一整个雨季 2021-01-30 07:34

What is the best way to write a state machine in C?
I usually write a big switch-case statement in a for(;;), with callbacks to re-enter the state machine when an external

9条回答
  •  梦谈多话
    2021-01-30 08:08

    I used this pattern. Is there a typical state machine implementation pattern? (check best answer).

    But i also add some features
    1. Information about previous state.
    2. Parameter passing
    3. Adding external events like global timeout and "resseting SM"

    I found state machines little less cryptic and maintainable.
    Anyway, I still think state machines are most difficult and annoying programming task.(I got so far)

提交回复
热议问题