// access to shared memory among threads // controlled using locks. Problem is that the programmer // is then responsible to locking and unlocking. // // The basic idea here is to start a bunch of thread and // have them all increment a single variable // The difference in the B version is that each thread increments // a local variable, then on completion updates the global shared memory // This reduces resource contention for the shared memory. // // Author: gtowell // Created: Dec 2023 package main import ( "fmt" "sync" ) var ( mu sync.Mutex counter int64 ) // this is the MUCH preferred way of locking in Go // guarantees that the unlock will happen! func ff(amt int64) { defer mu.Unlock() mu.Lock() counter += amt } func main() { var wg sync.WaitGroup threads:= 100 times := 1000000 for rz:=0; rz