...

/

Semaphore Pattern

Semaphore Pattern

This lesson explains an effective synchronization method that lets goroutines finish their execution first, also known as the semaphore pattern.

Introduction

We can also use a channel for synchronization purposes, effectively using it as what is called a semaphore in traditional computing. To put it differently, to discover when a process (in a goroutine) is done, pass it a channel with which it can signal it is ready. A common idiom used to let the main program block indefinitely while other goroutines run is to place select {} as the last statement in the main function. This can also be done by using a channel to let the main program wait until the goroutine(s) complete(s), the so-called semaphore pattern.

Explanation

The goroutine compute signals its completion by putting a value on the channel ch, and the main routine waits on <-ch until this value gets through.

On this channel, we would expect to get a result back, like in:

func compute(ch chan int) {
  ch <- someComputation() // when it completes, signal on the channel.
}
func main() {
  ch := make(chan int) // allocate a channel.
  go compute(ch) // start something in a goroutine
  doSomethingElseForAWhile()
  result := <-ch
}
...