Golang synchronization: Difference between revisions
From wikinotes
(Created page with "Synchronization tools are used to synchronize multiple concurrent codepaths so that your program can continue synchronously. = WaitGroups = <blockquote> WaitGroups are global...") |
(No difference)
|
Revision as of 19:05, 6 June 2022
Synchronization tools are used to synchronize multiple concurrent codepaths so that your program can continue synchronously.
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") }