C++: A Thorough Overview (2024-06-03)#

Day 1#

Introduction: What C++ >= 11 Brings#

Smart Pointers: std::shared_ptr<>, std::unique_ptr<> (And A Little std::move)#

From Smart Pointers: std::unique_ptr, std::shared_ptr (And std::weak_ptr):

Livecoding experiences:

Day 2#

Morning Awakening#

  • mutable on lambda, to capture value by non-const copy: weak-ptr.cpp

Unit Testing, Test Driven Development, And googletest#

A Little CMake#

From CMake (only what we use from it)

Exercise: class Sensor, And A SensorConfig Thereof#

  • Implementation detail: std::map

  • See tests directory in our Github project: jfasch/2024-06-03

    • Working: tests/sensor-const-suite.cpp

    • Working: tests/sensor-random-suite.cpp

    • Not working: tests/sensor-config-suite.cpp. Does not even compile ⟶ commented-out in tests/CMakeLists.txt

  • Comment-in tests/sensor-config-suite.cpp

  • Add implementation files toolcase/sensor-config.h and toolcase/sensor-config.cpp, and fix until tests run.

Multithreading: Threads, Race Conditions, Locking Primitives#

From Multithreading, C++ Memory Model:

Day 3#

Exercise: Make SensorConfig Thread Safe#

Based upon the exercise from Day 2,

Multithreading: Communication#

Day 4#

Exercise Time#

Group 1: Data Logger (Sketch)#

Given N Sensor instances in a SensorConfig, sketch a producer-consumer scenario over a Queue (the thread safe queue from queue.h),

  • N threads producing (timestamp, sensor_name, sensor_value) measurements into the queue.

  • One thread consuming those samples from the queue, and writing them to stdout as they arrive.

Group 2: Use std::latch#

  • Read the std::latch documentation

  • Given that communication device, and an understanding of it, write a program that pulls up a scenario that is just right for it. (“Construct a problem that fits the solution.”)

Group 3: Program a MyLatch Class#

Outcome:

$ ./programs/latch-demo 1.2 3.6 0.1 7.5
producer #3 arriving
producer #1 arriving
producer #2 arriving
producer #4 arriving
consumer says "yessss, everybody arrived"

RValue References, Move Semantics#