.. include:: Firmware: Collapse Three Threads Into One (Eventloop) ===================================================== .. topic:: See also * :doc:`../../polling/index` Problem: Too Many Threads ------------------------- * Stacksize not zero * |longrightarrow| Memory overhead .. image:: fsm-kill-threads.svg :scale: 80% This Can Be Done Better ----------------------- .. image:: fsm-eventloop.svg :scale: 80% Plan ---- * Collapse all in one thread * First: didactically poll (``take_noblock()``) each of the three semaphores * |longrightarrow| :doc:`event loop <../../polling/index>` to the rescue * Added benefit: remove mutex ``main()``: Single Thread, Waiting For Three Semaphores ------------------------------------------------------- .. literalinclude:: code/main.cpp :caption: :download:`code/main.cpp` :language: c++ FSM: Look Ma, No Mutex ---------------------- .. literalinclude:: code/fsm.h :caption: :download:`code/fsm.h` :language: c++ .. literalinclude:: code/fsm.cpp :caption: :download:`code/fsm.cpp` :language: c++