Skip to content

Instantly share code, notes, and snippets.

@mche
Created January 9, 2022 15:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mche/646954f3733674eee5883eff9b21a3f8 to your computer and use it in GitHub Desktop.
Save mche/646954f3733674eee5883eff9b21a3f8 to your computer and use it in GitHub Desktop.
Доп. запуск горутин из цикла результатов
package main
import (
"fmt"
"time"
//"math/rand"
)
func squares(i int, c chan int, semaphoreChan chan bool) {
semaphoreChan <- true
time.Sleep(time.Duration(10 * i) * time.Millisecond)//time.Duration(rand.Intn(500)*i)
c <- i
<- semaphoreChan
}
func main() {
fmt.Println("main() started")
c := make(chan int)
semaphoreChan := make(chan bool, 9)
defer func() {
close(c)
close(semaphoreChan)
}()
cnt := 0
for i := 0; i < 10; i++ {
go squares(i, c, semaphoreChan)
}
sum := 0
for val := range c {
fmt.Println(val)
cnt++
sum += val
if cnt == 30 {
break
} else /*if val == 1*/ {
go squares(val*5+1, c, semaphoreChan)
}
/*else {
time.Sleep(300 * time.Millisecond)
go squares(val*10, c, semaphoreChan)
}*/
}
fmt.Println("main() stopped", sum)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment