Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "math/rand"
- "runtime"
- "sync"
- )
- const NB_WORKERS = 3000
- const JOB_COUNTING_TO = 10000000
- var mu sync.Mutex
- var guard chan struct{}
- var nbConcurrentGoRoutines = 0
- var jobsDone = 0
- func max(a, b int) int {
- if a > b {
- return a
- }
- return b
- }
- func doJob(job int) {
- nbConcurrentGoRoutines = max(nbConcurrentGoRoutines, runtime.NumGoroutine())
- if rand.Intn(10) == 0 {
- return
- }
- for i := 0; i < job; i += 1 {
- }
- mu.Lock()
- jobsDone += 1
- mu.Unlock()
- var wg sync.WaitGroup
- for i := 0; i < 18; i += 1 {
- guard <- struct{}{}
- wg.Add(1)
- go func() {
- doJob(JOB_COUNTING_TO)
- wg.Done()
- }()
- }
- wg.Wait()
- <-guard
- }
- func main() {
- guard = make(chan struct{}, NB_WORKERS)
- doJob(JOB_COUNTING_TO)
- fmt.Println(jobsDone, nbConcurrentGoRoutines)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement