• Reusable barriers

    At the end of Let’s meet, I wrote: “The channel-based solution has one advantage going for it, though: try surrounding the body of the worker in a for loop.” This is what The Little Book of Semaphores calls a “reusable barrier” in section 3.6. It’s introduced with the following puzzle: Rewrite the barrier solution so that after all the threads have passed through, the turnstile is locked again. In other words, rewrite the previous solution so that once all goroutines pass, the next goroutine that arrives at the barrier doesn’t pass until all the other goroutines have arrived at the barrier again.
  • A semaphore by any other name…

    I skipped over “Chapter 2: Semaphores” because it doesn’t contain exercises. Nevertheless, looking back at it, I think it’s worth paying it a visit with Go in mind. The Little Book of Semaphores offers the following properties as a definition for a semaphore: When you create the semaphore, you can initialize its value to any integer, but after that the only operations you are allowed to perform are increment (increase by one) and decrement (decrease by one).
  • Let's meet

    Chapter 3 of The Little Book of Semaphores proposes the following puzzle: Generalize the signal pattern so that it works both ways. Thread A has to wait for Thread B and vice versa. If Thread A runs two statements a1 and a2, and Thread B runs two statements b1 and b2, the requirement is for a1 to execute before b2 and for b1 to execute before a2. In other words, the threads are to meet before executing their respective second statement.
  • Let's do lunch together… or not

    In the introduction to The Little Book of Semaphores (TLBOS), the author presents the following puzzle (rephrased a little): Imagine that Alice and her friend Bob live in different cities, and one day, around dinner time, Alice starts to wonder who ate lunch first that day, she or Bob. Assuming that Bob is willing to follow simple instructions, is there any way Alice can guarantee that tomorrow she will eat lunch before Bob?
  • The Little (Go) Book of Semaphores

    The Little Book of Semaphores, by professor Allen Downey, is a textbook that introduces the principles of synchronization for concurrent programming. It’s available under the GNU Free Documentation License. The book uses Python to illustrate the concepts, but I think Go is a much better fit for that purpose. Semaphores are rarely seen in Go as such. The sync package provides synchronization primitives, for example, mutual exclusion locks and condition variables, but notably it doesn’t provide a semaphore.