| 34 | ) |
| 35 | |
| 36 | func main() { |
| 37 | // Number of Goroutines to use. |
| 38 | const grs = 2 |
| 39 | |
| 40 | // wg is used to manage concurrency. |
| 41 | var wg sync.WaitGroup |
| 42 | wg.Add(grs) |
| 43 | |
| 44 | // Create two Goroutines. |
| 45 | for i := 0; i < grs; i++ { |
| 46 | go func() { |
| 47 | for count := 0; count < 2; count++ { |
| 48 | // Only allow one Goroutine through this critical section at a time. |
| 49 | // Creating these artificial curly brackets gives readability. We don't have to do |
| 50 | // this but it is highly recommended. |
| 51 | // The Lock and Unlock function must always be together in line of sight. |
| 52 | mutex.Lock() |
| 53 | { |
| 54 | // Capture the value of counter. |
| 55 | value := counter |
| 56 | |
| 57 | // Increment our local value of counter. |
| 58 | value++ |
| 59 | |
| 60 | // Store the value back into counter. |
| 61 | counter = value |
| 62 | } |
| 63 | mutex.Unlock() |
| 64 | // Release the lock and allow any waiting Goroutine through. |
| 65 | } |
| 66 | |
| 67 | wg.Done() |
| 68 | }() |
| 69 | } |
| 70 | |
| 71 | // Wait for the Goroutines to finish. |
| 72 | wg.Wait() |
| 73 | fmt.Printf("Final Counter: %d\n", counter) |
| 74 | } |