Synchronizing Goroutines with Channels

GolangGolangBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

This lab aims to test your knowledge of using channels to synchronize execution across goroutines.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL go(("`Golang`")) -.-> go/ConcurrencyGroup(["`Concurrency`"]) go/ConcurrencyGroup -.-> go/channels("`Channels`") subgraph Lab Skills go/channels -.-> lab-15458{{"`Synchronizing Goroutines with Channels`"}} end

Channel Synchronization

The problem to be solved in this lab is to create a goroutine that performs some work and notifies another goroutine when it's done using a channel.

To complete this lab, you will need to:

  • Create a function named worker that takes a channel of type bool as a parameter.
  • Inside the worker function, perform some work and then send a value to the channel to notify that the work is done.
  • In the main function, create a channel of type bool with a buffer size of 1.
  • Start a goroutine that calls the worker function and passes the channel as a parameter.
  • Block the main function until a value is received from the channel.
$ go run channel-synchronization.go
working...done

## If you removed the `<- done` line from this program, the
## program would exit before the `worker` even
## started.

There is the full code below:

// We can use channels to synchronize execution
// across goroutines. Here's an example of using a
// blocking receive to wait for a goroutine to finish.
// When waiting for multiple goroutines to finish,
// you may prefer to use a [WaitGroup](waitgroups).

package main

import (
	"fmt"
	"time"
)

// This is the function we'll run in a goroutine. The
// `done` channel will be used to notify another
// goroutine that this function's work is done.
func worker(done chan bool) {
	fmt.Print("working...")
	time.Sleep(time.Second)
	fmt.Println("done")

	// Send a value to notify that we're done.
	done <- true
}

func main() {

	// Start a worker goroutine, giving it the channel to
	// notify on.
	done := make(chan bool, 1)
	go worker(done)

	// Block until we receive a notification from the
	// worker on the channel.
	<-done
}

Summary

In this lab, you learned how to use channels to synchronize execution across goroutines. By creating a channel and passing it to a goroutine, you can wait for the goroutine to finish its work and notify you when it's done.

Other Golang Tutorials you may like