January 2020
Intermediate to advanced
640 pages
16h 56m
English
A barrier can be thought of as a rendezvous point for a set of processes. Once a process enters the barrier, it is prevented from making any progress until all other expected processes also enter the barrier.
In Go, we could model a barrier with the help of the sync.WaitGroup primitive, as follows:
func barrier(numWorkers int) { var wg sync.WaitGroup wg.Add(numWorkers) for i := 0; i < numWorkers; i++ { go func() { wg.Done() fmt.Printf("Entered the barrier; waiting for other goroutines to join") wg.Wait() fmt.Printf("Exited the barrier") }() } wg.Wait()}To guarantee that each worker executes the various stages of the graph state machine in lock-step with the other workers, we must ...