Created
August 8, 2021 02:10
-
-
Save karngyan/a99b398a834ac92011759b97f4d6f7dc to your computer and use it in GitHub Desktop.
Mutex/RWMutex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"math" | |
"os" | |
"sync" | |
"text/tabwriter" | |
"time" | |
) | |
func producer(wg *sync.WaitGroup, l sync.Locker) { | |
// sync.Locker is an interface with 2 methods: Lock, Unlock | |
// which Mutex and RWMutex respects | |
// You can read about duck typing in Go to understand this concept | |
defer wg.Done() | |
for i := 5; i > 0; i-- { | |
l.Lock() | |
l.Unlock() | |
time.Sleep(1) | |
} | |
} | |
func observer(wg *sync.WaitGroup, l sync.Locker) { | |
defer wg.Done() | |
l.Lock() | |
defer l.Unlock() | |
} | |
func test(count int, mutex, rwMutex sync.Locker) time.Duration { | |
var wg sync.WaitGroup | |
wg.Add(count + 1) | |
beginTime := time.Now() | |
go producer(&wg, mutex) | |
for i := count; i > 0; i-- { | |
go observer(&wg, rwMutex) | |
} | |
wg.Wait() | |
return time.Since(beginTime) | |
} | |
func main() { | |
tw := tabwriter.NewWriter(os.Stdout, 0, 1, 2, ' ', 0) | |
defer tw.Flush() | |
var m sync.RWMutex | |
fmt.Fprintf(tw, "Readers\tRWMutex\tMutex\n") | |
for i := 0; i < 25; i++ { | |
count := int(math.Pow(2, float64(i))) | |
fmt.Fprintf(tw, "%d\t%v\t%v\n", count, test(count, &m, m.RLocker()), test(count, &m, &m)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment