szaydel
9/19/2018 - 6:22 PM

Example of multiple reads lock in Golang

package main

import (
	"fmt"
	"sync"
	"time"
)

func noop(l *sync.RWMutex, wg *sync.WaitGroup) {
	l.RLock()
	<-time.NewTimer(1 * time.Second).C
	fmt.Printf(".")
	wg.Done()
}

func main() {
	var lock *sync.RWMutex
	lock = new(sync.RWMutex)
	var wg = &sync.WaitGroup{}

	for i := 0; i < 100; i++ {
		wg.Add(1)
		go noop(lock, wg)
	}
	
	wg.Wait()

	fmt.Printf("\n%v\n", lock)
}