Golang synchronization
From wikinotes
Synchronization tools are used to synchronize multiple concurrent codepaths so that your program can continue synchronously.
Documentation
sync
docshttps://pkg.go.dev/sync
Mutexes
RWMutex
Variation of a mutex where:
- any number of items can acquire a lock for reading
- only one item can acquire lock for writing
- you cannot write while any other lock is reading
import "sync" var lock = RWMutex{} // lock/unlock for reading lock.RLock() lock.RUnlock() // lock/unlock for writing lock.Lock() lock.Unlock()It would be sensible to unlcok in
defer
red functions.
WaitGroups
WaitGroups are global threadsafe counters that are incremented/decremented.
wg = sync.WaitGroup{} wg.Add(3) // add 3x increments to countdown wg.Done() // decrement waitgroup by one wg.Wait() // wait for waitgroup to reach 0// wait-groups are threadsafe proxy objects, intended to be globally accessible var wg = sync.WaitGroup{} func printHi() { fmt.Println("hi") wg.Done() } func main() { wg.Add(3) for i=0; i<3; i++ { go printHi() } wg.Wait() fmt.Println("bye") }