Concurrency Model of Go Language

It is known that concurrent execution of more than one task, or multi-threading, is often used in programming. This enables a simultaneous launch of many smaller components (for example, processing of multiple requests simultaneously, in the case of a web server. One of the problems that Go’s authors tried to solve during the process of […]

Category

Technologies

Posted

Eugene

Feb 13, 2018

go routine | Swan Software Solutions

It is known that concurrent execution of more than one task, or multi-threading, is often used in programming. This enables a simultaneous launch of many smaller components (for example, processing of multiple requests simultaneously, in the case of a web server. One of the problems that Go’s authors tried to solve during the process of creation was to facilitate programming for multi-core systems. That is why Go language uses light flows, so-called Go routines, and channels as a means of interaction between them. In addition to the channels there are sync primitives like mutexes in the sync package. A multi-threading model in Go was created on the basis of Communicating Sequential Processes (CSP) by Tony Hoare, published in 1978 at the Association for Computing Machinery, and is based on previous parallelizing.

What is Go routine

Go routine is a function that can work in parallel with other functions. According to the documentation: a Go routine is part of making concurrency easy to use. The idea, which has been around for a while, is to multiplex independently executing functions—coroutines—onto a set of threads. When a coroutine blocks, such as by calling a blocking system call, the runtime automatically moves other coroutines on the same operating system thread to a different, runnable thread so they won’t be blocked. The programmer sees none of this, which is the point. The result, which we call a Go routine (s), can be very cheap: they have little overhead beyond the memory for the stack, which is just a few kilobytes.

Go allows you to create a new stream of program execution (go-procedure) using the reserved keywords go.  The  keyword go launches the function in another, newly created go-procedure. All go-procedures in one program use the same address space. Inside, go-procedures act as subroutines, separated in different streams in the operating system. When writing multithreading applications in most cases it is always necessary to work with general data as their simultaneous change  can lead to unpleasant consequences.

How it works

The channels provide the ability to communicate one after another in order to synchronize the performance of several goroutines. Channels in Go are divided in types and are objects of the first class. They can be sent to functions, rotated from functions, appropriated to variables and fields of structures, and also sent  via channels. They can work in one or two directions. Moreover, if a typical bi-directional channel is transferred to the function where it is used (for example, only for reception), it can be marked appropriately in the list of function parameters. The compiler checks  its correct use. Ordinary channels block goroutine that tries to receive or send something to the channel,  while the correspondent reverse action will not be executed  from the other side. But the channel may have a buffer that accumulates and gives back a certain number of meaning ​​without blocking.

What needs to be taken into account

The channel can be closed by the built-in function close, though the reference to its values  ​​will lead to panic. If the closed channel has a non-empty buffer, these values ​​can still be obtained. But when the buffer  contains little information or no information at all, the closed channel when trying to read it will give out  zero values ​​of those  types it was created for. That is, the closed data channels will produce empty lines, integer channel – zeros, indexes channel – nil ( reserved keywords).

It should be noted that by default Go functions on the same thread, using its own scheduler and asynchronous calls. In this case, the channels are running very fast. But if you set the use of 2 or more threads, then Go begins to apply blocking and the speed may fall.

Conclusion

It is quite convenient and simple to write concurrent and multi-threaded programs on Go, which is thought by many to be the best way to write Go program is  on the base of  non buffered channels, and consequently it is required to  add the  buffer where it will increase the speed. This approach allows you to immediately find a deadlock, which can appear at the most inappropriate time.

To learn more, contact the professionals at Swan Software Solutions today. Our experts are prepared to discuss what software development, including Go language, can do for your business.