Skip to content

Instantly share code, notes, and snippets.

@humamfauzi
Last active August 13, 2018 07:46
Show Gist options
  • Save humamfauzi/0b941dcb7ef4eacfd99695e29360721e to your computer and use it in GitHub Desktop.
Save humamfauzi/0b941dcb7ef4eacfd99695e29360721e to your computer and use it in GitHub Desktop.
Basic Usage of WaitGroup
package main
import (
"time"
"math/rand"
"fmt"
"sync"
)
func randomArray(max int, n int) []int {
var wg sync.WaitGroup
array := make([]int, n)
wg.Add(n)
go func() {
for k := range array {
array[k] = rand.Intn(max)
wg.Done()
}
}()
wg.Wait()
return array
}
func main() {
size := 1 << 20
totalCategory := 4
rand.Seed(time.Now().Unix())
var wg sync.WaitGroup
array := randomArray(totalCategory, size)
categoryCount := make([]int, totalCategory)
wg.Add(size)
go func() {
for _, v := range array {
categoryCount[v]++
wg.Done()
}
}()
wg.Wait()
for k, v := range categoryCount {
fmt.Println(k, " :: ", v, " RATIOS :: ", float32(v)/float32(size))
}
}
package main
import (
"time"
"math/rand"
"fmt"
"sync"
)
func randomArray(max int, n int) []int {
array := make([]int, n)
go func() {
for k := range array {
array[k] = rand.Intn(max)
}
}()
return array
}
func main() {
size := 1 << 20
totalCategory := 4
rand.Seed(time.Now().Unix())
var wg sync.WaitGroup
array := randomArray(totalCategory, size)
categoryCount := make([]int, totalCategory)
wg.Add(size)
go func() {
for _, v := range array {
categoryCount[v]++
wg.Done()
}
}()
wg.Wait()
for k, v := range categoryCount {
fmt.Println(k, " :: ", v, " RATIOS :: ", float32(v)/float32(size))
}
}
package main
import (
"time"
"math/rand"
"fmt"
)
func randomArray(max int, n int) []int {
array := make([]int, n)
go func() {
for k := range array {
array[k] = rand.Intn(max)
wg.Done()
}
}()
return array
}
func main() {
size := 1 << 20
totalCategory := 4
rand.Seed(time.Now().Unix())
array := randomArray(totalCategory, size)
categoryCount := make([]int, totalCategory)
go func() {
for _, v := range array {
categoryCount[v]++
}
}()
for k, v := range categoryCount {
fmt.Println(k, " :: ", v, " RATIOS :: ", float32(v)/float32(size))
}
}
package main
import (
"time"
"math/rand"
"fmt"
)
func randomArray(max int, n int) []int {
array := make([]int, n)
for k := range array {
array[k] = rand.Intn(max)
}
return array
}
func main() {
size := 1 << 20
totalCategory := 4
rand.Seed(time.Now().Unix())
array := randomArray(totalCategory, size)
categoryCount := make([]int, totalCategory)
for _, v := range array {
categoryCount[v]++
}
for k, v := range categoryCount {
fmt.Println(k, " :: ", v, " RATIOS :: ", float32(v)/float32(size))
}
}

This is example how wait group works. Main objective of wait group is control concurrency. In vanilla.go, we have ordinary program without concurrency. The goal of each program is creating numbers of random integer, count total numbers of each numbers and print it with its ratios respective to total numbers created.

When we apply go goroutine, Go does not wait for the process to complete to proceed (non-blocking). While it is a good thing for execution speed, non-blocking process can be messed up as in no_waitgroup. Here come the wait group. Wait group have three methods which is .Add(), .Done(), Wait(). The first we use .Add() with input of how many process must be done before waiting is over. .Done() is a way telling the wait group that a process has been done. .Wait() halt the process unless all process done which indicated by number of .Done() executed is equal to .Add() input.

Full application of goroutine and wait group in final_waitgroup.go

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment