Concurrency with Goroutines
Let us modify the simple blink program from our earlier tutorial to run multiple tasks simultaneously. This is what we call concurrency.
Go has a number of primitives to help make programmers' lives easier when they create concurrent programs. In this tutorial, we will look at one of them, namely Goroutines.
The Program
The aim of our program is to do the following two things at once:
- Blink the onboard LED twice a second.
- Print the phrase "hello concurrently" to the serial (UART) port.
We modify the blink.go
program from the
earlier tutorial to add:
- A function (named
printHello
) that prints "hello concurrently" in an infinite loop. - A goroutine that spawns this function so that it runs concurrent to the main thread that is blinking the LED.
// ConcurrentBlink.go
package main
import (
"machine"
"time"
)
func main() {
led := machine.LED
led.Configure(machine.PinConfig{
Mode: machine.PinOutput,
})
go printHello()
for {
led.High()
time.Sleep(500 * time.Millisecond)
led.Low()
time.Sleep(500 * time.Millisecond)
}
}
func printHello() {
for {
println("hello concurrently")
time.Sleep(time.Second)
}
}
Testing the Program
Ensure that your serial debugger is set up as described in my earlier tutorial on setting up the Pico for serial debugging.
Flash the program to your pico:
% tinygo flash -target=pico concurrentblink.go
You should see the onboard LED blinking, while the phrase "hello concurrently" is printed every second in the serial monitor.
References
- Rob Pike's presentation on "Concurrency is not parallelism".
Subscribe to my newsletter
Read articles from Charath Ranganathan directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Charath Ranganathan
Charath Ranganathan
I am an electrical engineer by training, and a software developer and manager by profession. I started coding after middle school (in the late 1980s) on a Sinclair ZX Spectrum home computer. As this is my personal blog, I focus on my hobby interests, i.e. microcontrollers, embedded systems, and electronics.