I have tried to recreate a situation below, wherein I am trying to run three functions concurrently. However, I can only run the second and third function after a certain point in the first function, since it depends on a value from the first function. For this case, I used a channel. Here's my code:
package code
import "fmt"
func First(c chan string) {
for i := 0; i < 10000; i++ {
continue
}
test := "test"
fmt.Println(test)
c <- test
for i := 0; i < 10000; i++ {
fmt.Print(i)
}
}
func Second(c chan string) {
msg:= <- c
fmt.Println(msg)
if msg != "" { // I need to run the whole of First before running second and third concurrently then
fmt.Println("second", msg)
for i := 0; i < 10000; i++ {
fmt.Print(i)
}
}
}
func Third(c chan string) {
msg:= <- c
fmt.Println("third", msg)
for i := 0; i < 10000; i++ {
fmt.Print(i)
}
}
package main
import (
"./code"
"fmt"
"sync"
//"sync"
"time"
)
func main() {
start := time.Now()
var wg sync.WaitGroup
wg.Add(3)
var c chan string = make(chan string)
go func() {
code.First(c)
wg.Done()
}()
go func() {
code.Second(c)
wg.Done()
}()
go func() {
code.Third(c)
wg.Done()
}()
wg.Wait()
end := time.Now()
delta := end.Sub(start)
fmt.Println("time", delta.Seconds())
}
Currently, I am getting a deadlock. A couple extra questions- is there a way to first check the values given by the channel, and if they are not certain expected values I first complete running the first function and then run second and third concurrently? Essentially a check like the one I added in second? If it is the expected value I'd like all the functions to run concurrently after the channel gets particular values from the first function.