-1

So, I am testing Golang. I understand fmt.Println is not thread safe. So, I was trying out sync.Mutex. The following is the program:

func threder(mux *Mutex, i int) {
    mux.Lock()
    fmt.Println("I am thread: ", i)
    mux.Unlock()
    return
}

func main() {
    m := &Mutex{}
    for i := 0; i < 300; i++ {
        go threder(m, i)
    }
}

I am expecting 300 lines of output. But, I am getting 80-90 lines. Where am I wrong?

Chris Roy
  • 945
  • 1
  • 7
  • 19

1 Answers1

4

When your main returns, all other threads are killed. As your main returns right after starting all threads, some of them don't get to print. You can use sync.WaitGroup to wait for all threads to stop:

func main() {
    m := &sync.Mutex{}
    var wg sync.WaitGroup
    for i := 0; i < 300; i++ {
        wg.Add(1)
        go func(i int) {
            defer wg.Done()
            threder(m, i)
        }(i)
    }
    wg.Wait()
}

About your comment: If you do not pass the sync.Mutex as a pointer, but the value, to the function the sync.Mutex will be copied and each copy will operate independently of each other. Therefore there will be no synchronization between threads, as they all use their own, independent sync.Mutex. Also, you must not copy a sync.Mutex after the first use, as stated in the docs. This would not happen in your case, as you copy before using the sync.Mutex, but be aware of it.

Leon
  • 2,926
  • 1
  • 25
  • 34
  • I have one more question. What happens if I don't pass a pointer to the mutex i.e. instead of &Mutex{}, if I just do Mutex{}? – Chris Roy Oct 09 '18 at 09:54