.. include:: 2026-11-03 (3): Multithreading, Project Talk ============================================ Multithreading, And Race Conditions ----------------------------------- .. _fh-2026-11-03-mt: Doing Multiple Things In Parallel ................................. .. topic:: See also * :doc:`/trainings/material/soup/cxx/cxx11/multithreading/020-thread/topic` Here's a sketch running the PLC loop and the commandline loop in parallel, using multithreading. .. literalinclude:: code/explain.cpp :caption: :download:`code/explain.cpp` :language: c++ The Consequences Of Doing Multiple Things In Parallel ..................................................... .. topic:: See also * :ref:`cxx-memory-model-integer-increment-single-thread` * :ref:`cxx-memory-model-integer-increment-not-atomic` * :ref:`cxx-memory-model-integer-increment-mother` * :ref:`cxx-memory-model-integer-increment-lms` Livehacked ... .. literalinclude:: code/mother-of-all-race-conditions.cpp :caption: :download:`code/mother-of-all-race-conditions.cpp` :language: c++ * Expected output: 20000000 * Actual output (varies): 10475662 .. code-block:: console $ ./2026-11-03-mother-of-all-race-conditions the_number: 10475662 Project: First Tasks -------------------- Door Finite State Machine ......................... Test driven development of the FSM class in the middle. Cornerstones: * Knows the motor (see :ref:`below `) |longrightarrow| defines the interface * Does *not* know any button. Rather, provides methods (like, "dwim pressed", "open position reached", "closed position reached") to inject events that *might* have their origins in buttons - but not necessarily. (See :ref:`fh-2026-11-03-cmdline`) .. _fh-2026-11-03-buttons: Buttons: Edge Detection ....................... ``Button`` interface is level based - you ask it whether its state is "pressed" or "released". The FSM wants *events* that we need compute from the levels, in a 1ms PLC polling loop. Around a ``Button``, wrap another object that can be asked if an edge (configurable: rising, falling) has been detected. UML might look like this: .. plantuml:: @startuml interface Button { + is_pressed() } class MockButton {} class LibGPIODButton {} Button <|.. MockButton Button <|.. LibGPIODButton class EdgeDetector { + is_edge() } EdgeDetector -r-> Button @enduml *Keep an eye on followup task "debouncing"*. Hardware: GPIO On The Pi ........................ * Setup a Pi * Do some research on ``libgpiod`` (a library to control GPIOs on Linux) * Focus on immediate feature (see :ref:`fh-2026-11-03-buttons`): input, level based. Goal is to implement ``LibGPIODButton`` in the UML diagram from :ref:`fh-2026-11-03-buttons` * Keep an eye on hardware based edge detection and debouncing which will be one of the next steps .. _fh-2026-11-03-cmdline: Door Commandline ................ Create a program that can be used to manually test the FSM. That program ... * Instantiates a dummy Motor implementation (derived from interface ``Motor`` as defined by the FSM). As a first step, this implementation would simply output something like "left", "right", "stop" in calls to the methods with the same name. * Instantiates a FSM instance which *knows* the motor. * Reads from stdin in a loop, like ``cmdline_loop()`` in the sample program in :ref:`fh-2026-11-03-mt`. The program does not use multithreading like the sample program does. Check how the ``readline`` library can be used in place of raw ``read(STDIN_FILENO, ...)``, to provide history browsing. * Understands commands that drive the FSM, or report state, like ... * DWIM button pressed * Open position reached * Closed position reached * Report motor state (left, right, stopped) * Report FSM state * ... .. _fh-2026-11-03-motor: Motor ..... The door's stepper motor is controlled by a A4988 chip. The A4988 requires two GPIO pins configured as output for direction/enable, and a PWM pin for the motor speed. * **Goal 1**: define interfaces for ``Switch`` and ``PWMPin`` * **Goal 2**: based on mock implementations of those, write a number of tests. * **Goal 3**: make tests run. .. plantuml:: @startuml interface Motor { + set_direction() } interface Switch { + set_state() } interface PWMPin { + set_period() + set_duty_cycle() } A4988_Motor -up-|> Motor A4988_Motor -right-> "direction 1" Switch A4988_Motor -right-> "enable 1" Switch A4988_Motor -right-> PWMPin @enduml