Skip to content
Quiz · Goroutines and channels · lesson 10 of 13

Quiz — a counter under a Mutex

What does the program print?

GO
package main

import "fmt"
import "sync"

func main() {
    var wg, mu, n = sync.WaitGroup{}, sync.Mutex{}, 0
    wg.Go(func() { mu.Lock(); n++; mu.Unlock() })
    wg.Go(func() { mu.Lock(); n++; mu.Unlock() })
    wg.Wait()
    fmt.Println(n)
}

Subscribe to take the course

The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.

Sign in another way