0
Main {
go routine_1(carryout a time consuming task and return output)
go routine_2(wait for output from routine_1, collect output and do task_2)
go routine_3(wait for output from routine_1, collect output and do task_3)
wait for all routines to complete
}

I have used channel, but once routine_2 reads the data routine_3 doesn't get the data.

I dont want the main thread to do the syncronization i.e to know the dependencies between routine_2,routine_3 to routine_1

Turns out closing the channel will give the needed broadcast for the other routines to know the result is ready.

Thank you for your time.(This is my first question in stackoverflow, really happy to see such good quality and quick responses) Thanks to all of you.

Updating with the answer that I picked. thanks again.

package main

import (
    "fmt"
    "sync"
)

func computeResult() int {
    return 100
}

func main() {

    var wg sync.WaitGroup
    wg.Add(2)
    var output int

    ch := make(chan struct{})

    // Compute result
    go func() {
        defer wg.Done()
        output = computeResult()
        fmt.Println("closing channel to signal task done")
        close(ch)
        fmt.Println("channel closed")

    }()

    go func() {
        defer wg.Done()
        // Wait for ch close
        <-ch
        fmt.Println(output)
    }()

    wg.Wait()

    var wg2 sync.WaitGroup
    wg2.Add(1)

    go func() {
        defer wg2.Done()
        fmt.Println("wait on closed channel")
        // Wait for ch close
        <-ch
        fmt.Println(output)
    }()

    wg2.Wait()

}
5
  • Create separate channels for routine_2 and routine_3, and send the same output to both. Commented Oct 11, 2019 at 18:25
  • I dont want the main thread to do the syncronization i.e to know the dependencies between routine_2,routine_3 to routine_1 Commented Oct 11, 2019 at 18:57
  • The pointer to related question is also suggesting to use close on a channel to broadcast that the output is ready. Commented Oct 11, 2019 at 19:15
  • @kumar you should accept and answer (green checkmark) - rather then edit your question to include an answer. You can also answer your own question with your own answer - but one should only do this if the answer is radically different than the ones on offer. Commented Oct 11, 2019 at 20:08
  • I see, thank you. used the green checkmark to accept the answer. Commented Oct 11, 2019 at 20:41

3 Answers 3

3

You can just call other two routines after first one is finished:

var wg sync.WaitGroup
wg.Add(2)
go func() {
    output := task1()
    go func() {
        defer wg.done()
        task2(output)
    }
    go func() {
        defer wg.done()
        task3(output)
    }
}()
wg.Wait()
Sign up to request clarification or add additional context in comments.

4 Comments

And to wait for task2 and task3 you can use a WaitGroup, for instance.
this does not wait for the tasks to complete
I dont want the main thread to do the syncronization i.e to know the dependencies between routine_2,routine_3 to routine_1
simple but elegant. thx
0

You can use the closing of a channel to broadcast result readiness, and use a shared variable to store the result:

func() {
   var output SomeType
   ch:=make(chan struct{})

   // Compute result
   go func() {
      output=computeResult()
      close(ch)
   }()

   go func() {
     // Wait for ch close
     <-ch
      useOutput()
    }()
}

You can have many goroutines waiting for the channel to close. When the channel closes, all goroutines will be enabled.

1 Comment

Turns out closing the channel will give the needed broadcast for the other routines to know the result is ready. This seem to be working. play.golang.org/p/aeLSAdWY2H6
0

You can do it ways like.. -create task followed by result -result use as another task.. -use waitgroup(prperty sync package) mainly for wait functionlity.. -use lamda function and time interval(time.sleep(time.sec/min). -Here based on task dependency use Mutex(lock/unlock)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.