Grokking Concurrency in Go
Vote

0% completed

​

Mutex: Protecting Shared State

The program below starts 100 goroutines. Each one adds 1 to a shared variable 1,000 times, so the final count should be 100,000.

Five runs on one machine printed these totals:

count: 93861 count: 100000 count: 97065 count: 97088 count: 70949

One run was correct and four were short, the worst by almost 30,000. The program waits for every goroutine with a done channel, as in Waiting for Goroutines with Done Channels, so no goroutine is cut off early. The increments themselves are being lost.

.....

.....

.....

Like the course? Get enrolled and start learning!

Reading Progress

0%


Vote for new content