I am trying to use Broadcast() function from "sync" but it does not work as i wish to.
I need to lock execution for all of the goroutines and then simply by calling C.Broadcast() release them and let them execute but it is not happening.
How can i make it work?
This is everything what is written in docs **Broadcast wakes all goroutines waiting on *sync.Cond. **
Here is the code i am trying to make work:
package main
import (
"fmt"
"sync"
"time"
)
var M sync.Mutex = sync.Mutex{}
var C *sync.Cond = sync.NewCond(&M)
func ff(){
M.Lock()
C.Wait()
fmt.Println("broadcasting")
M.Unlock()
}
func main() {
num := [6]int{}
for _, _= range num {
go ff()
}
M.Lock()
C.Broadcast()
M.Unlock()
fmt.Println("done")
time.Sleep(5 * time.Second)
}