Running multiple go routines at the same time -


i want run multiple go routines. want them start same time. added sync waitgroup , added wait inside start of go routine. did not work have go routine start @ same time. should in order have number of go routines start @ same time?

package main  import (         "flag"         "fmt"         "sync"         "time" )  func main() {      var wg sync.waitgroup       routines := flag.int("runs", 100, "routines running")       flag.parse()        wg.add(*routines)        := 0; < *routines; i++ {               go func() {                       defer wg.done()                       t := time.now()                       fmt.printf("%s\n", t)               }()       }       fmt.println("waiting finish")       wg.wait() 

you can have goroutines block on channel, , close channel once dispatched:

start := make(chan struct{})  := 0; < *routines; i++ {     go func() {         <-start         defer wg.done()         t := time.now()         fmt.printf("%s\n", t)     }() } fmt.println("starting") close(start) 

this close "exactly same time" possible. can't guarantee run @ precisely same time, , if there more goroutines gomaxprocs or cpu cores, can't possibly run @ same time.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -