Multithreaded programming in Go

Photo by Lewis Kang'ethe Ngugi on Unsplash

Concurrency, in general, refers to incidents or situations that occur or arise at the same time.

Concurrent programming is a programming technique in which two or more processes begin, run in an interleaved fashion through context switching, and finish in an overlapping time span by controlling access to shared resources, such as a single-core CPU.

This does not imply that many processes may be operating simultaneously, even though the results will seem to imply so.

What is the difference between concurrent and parallel programming?

Parallel processing is accomplished in parallel computing by hardware parallelism, such as running two operations simultaneously on two different CPU cores.

Concurrent Programming In Go

With the go declaration, you can launch a new thread of execution, known as a goroutine. It executes a function in a newly developed goroutine. All goroutines use the address space in a single program.

The software below will print “Hello from main goroutine” Depending on which of the two goroutines finishes first, and it can also print “Hello from another goroutine.”

The following software would almost certainly print both “Hello from the main goroutine” and “Hello from another goroutine.” They can be printed in whatever order you choose. Another explanation is that the second goroutine is very sluggish and does not print the message until the program terminates.

Here’s a more practical example in which we describe a function that uses concurrency to delay a broadcast.

Let’s implement it.

Let’s run this example, and if everything works, this will be the output.

$ go run main.go
Something is happening here (before broadcast) *people are just living or something*
Broadcasting Message: A New Broadcasting Message Using A Thread
If everything worked, the message has been sent 7 seconds ago

Channels

A channel is a framework for goroutines to interact and synchronize execution by transferring values. The built-in function make can be used to create a new channel value.

To transmit a value over a channel, use the binary operator ->. Use it as a unary operator to receive a value on a channel. The <- operator indicates whether the channel is sending or receiving data. The channel is bi-directional if no direction is defined.

The near role indicates that no further values can be transmitted over a channel. It is important to note that closing a channel is only needed if a receiver is searching for a close.

In the following example, we let the Broadcast function return a channel used to broadcast a message when the text is written.

What Are Data Races?

A data race happens when two goroutines contact the same variable simultaneously, with at least one of the accesses being a writer. Data races are prevalent and can be very difficult to debug.

There is a data race in this function, and its action is undefined. E.g., it might print the number 1. Try to find out if it is true — one possible answer follows the code.

In Go, the recommended method for handling parallel data access is to use a channel to transfer individual data from one goroutine to the next.

Killing A Go Routine

Make a goroutine stoppable, listen for a stop signal on a dedicated quit channel, and search this channel at appropriate points in the goroutine.

References


How To Handle Multi-Threaded Programming: Learning Concurrent Programming In Go. was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.