Comments by "LoneTech" (@0LoneTech) on "Santa gave me a concurrency problem for Christmas..." video.
-
22
-
5
-
This exercise is designed to demonstrate the flexibility of semaphores and test students' understanding of them. Unfortunately, it revealed flaws in your understanding, not a great point to start teaching from.
This video didn't describe the challenge very well. Sadly, neither did the original paper. This is why you both missed relevant points of synchronization; in the original paper, santa may finish deliveries before the reindeer arrive at the sleigh. In yours, the sleigh does not exist and the reindeer may go back on vacation before santa finishes his deliveries.
The "sneaky bug" reveals a notable misunderstanding of what semaphores do. This bug is imaginary, as semaphores can hold a number of posts. Santa posted 9 times, so 9 times a reindeer will get out. It doesn't matter that santa moved on, the count is in the semaphore; like a counting turnstile. The behaviour you worried about could occur with POSIX condition variables, where you can only wake already waiting threads.
The use of mutexes missed the point that semaphores can do that too (it's why C++ named their methods release and acquire, instead of signal and wait). Perhaps more concerning is that you overserialized independent blocks by sharing the lock for different variables.
The explanatory value of flashing up miniature pictures of large chunks of code, that we have neither the time nor resolution to read, is doubtful.
4
-
3
-
It's a deep topic. At its root is atomicity, which means something that won't be interrupted (may still be more than one cycle, but nothing affecting it should overlap). The precise mechanisms vary in complexity, but most CPUs have a feature where a sufficiently complicated operation can be performed without interruptions, even from other CPU cores (e.g. the LOCK signal on 386). Compare-and-swap is one of the easier to understand, and load-link/store-conditional is a more flexible variant. These aren't limited to CPUs either; there are memory chips that support operations like atomic increment, such that the value need not be loaded into the processor. For many programs, the entire concept can be abstracted away; STM (software transactional memory) allows focusing on the logic rather than the sequence. Some atomics are quite expensive, e.g. serializing all threads in a GPU work group or CPUs contending for one cache line (which may take hundreds of cycles to load), while others can be very fast (bitwise and/or in a workgroup can be as fast as a broadcast).
A mutex is very simple with compare and swap: Acquire is compare to unlocked, if equal swap to locked, if not yield and repeat.
3
-
3
-
2
-
2
-
2
-
2
-
2
-
2
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
They are indeed distinct, and there are other definitions too. For instance, threading predates multithreading; it's when a set of instructions are assembled into one subprogram, like beads on a string, as used in threaded Forth implementations.
Concurrency is multiple things (co-) existing at the same time (current). Multithreading or multiprocessing is when we have more than one thread of control in the same system. Parallelism usually refers to when processing might be concurrent, i.e. more than one thread is actually running (as opposed to time sharing), whether through SMT, SIMD, SMP, coprocessing or distribution; typically to speed computation up.
In this example, no requirement of parallelism is present, but asynchronous concurrent multithreading is assumed. Santa is an example of single thread concurrency in that he may be awakened for multiple tasks. A more typical example might be a chat program awaiting either user or network input.
It's a synchronization challenge, with unclear requirements, where you're expected to use shared memory and only the one synchronization tool (semaphores). In modern software (from about a decade before this problem was posed) we'd probably use a higher level construct, such as MVar or channels, to associate the relevant data (e.g. count in room or wakeup source) with the event.
1