Go Concurrency: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 9: Line 9:
Go takes a different approach. The fundamental concurrent execution primitive is the [[Go_Language_Goroutines#Overview|goroutine]], exposed by the language with the [[Go_Language#Keywords|language keyword]] <code>[[Go_Language#go_keyword|go]]</code>. Goroutines are managed by the Go runtime, which maps them transparently onto threads. While Go provides memory access and thread synchronization primitives at the language level via the <code>[[Go_Package_sync|sync]]</code> package, language guidelines do not exactly encourage their use, unless in very specific situations, such as within a small <code>struct</code> scope. Go provides an alternative concurrency programming model in language, based on [[Concurrent_(Parallel)_Programming#Communicating_Sequential_Processes_(CSP)|Communicating Sequential Processes (CSP)]], a theoretical model introduced by Anthony Hoare in the "Communicating Sequential Processes" 1978 ACM paper. This concurrency programming model uses [[Go Language Channels#Overview|channels]], which are exposed at language level with the <code>[[Go_Language#chan_keyword|chan]]</code> keyword.
Go takes a different approach. The fundamental concurrent execution primitive is the [[Go_Language_Goroutines#Overview|goroutine]], exposed by the language with the [[Go_Language#Keywords|language keyword]] <code>[[Go_Language#go_keyword|go]]</code>. Goroutines are managed by the Go runtime, which maps them transparently onto threads. While Go provides memory access and thread synchronization primitives at the language level via the <code>[[Go_Package_sync|sync]]</code> package, language guidelines do not exactly encourage their use, unless in very specific situations, such as within a small <code>struct</code> scope. Go provides an alternative concurrency programming model in language, based on [[Concurrent_(Parallel)_Programming#Communicating_Sequential_Processes_(CSP)|Communicating Sequential Processes (CSP)]], a theoretical model introduced by Anthony Hoare in the "Communicating Sequential Processes" 1978 ACM paper. This concurrency programming model uses [[Go Language Channels#Overview|channels]], which are exposed at language level with the <code>[[Go_Language#chan_keyword|chan]]</code> keyword.


This approach allows you to directly map concurrent problems into concurrent constructs instead of dealing with the minutia of starting and managing threads, and mapping logic evenly across available threads. In languages that expose threads, you need to worry about things like thread pools, and map requests onto threads. In contrast, in Go you would write a function and prepend its invocation with the <code>go</code> keyword. The runtime handles everything else automatically.
This approach allows you to directly map concurrent problems into concurrent constructs instead of dealing with the minutia of starting and managing threads, and mapping logic evenly across available threads. In Go, programmers model concurrency in goroutines, as concurrent functions, and channels, and communication mechanisms between parallel functions. In languages that expose threads, you need to worry about things like thread pools, and map requests onto threads. In contrast, in Go you would write a function and prepend its invocation with the <code>go</code> keyword. The runtime handles everything else automatically.


=Goroutines=
=Goroutines=

Revision as of 22:52, 15 January 2024

Internal

Overview

The majority of programming languages address concurrency by providing a representation of O/S threads or green threads directly in the language or in libraries, and providing memory access synchronization primitives to protect data in presence of concurrent access.

Go takes a different approach. The fundamental concurrent execution primitive is the goroutine, exposed by the language with the language keyword go. Goroutines are managed by the Go runtime, which maps them transparently onto threads. While Go provides memory access and thread synchronization primitives at the language level via the sync package, language guidelines do not exactly encourage their use, unless in very specific situations, such as within a small struct scope. Go provides an alternative concurrency programming model in language, based on Communicating Sequential Processes (CSP), a theoretical model introduced by Anthony Hoare in the "Communicating Sequential Processes" 1978 ACM paper. This concurrency programming model uses channels, which are exposed at language level with the chan keyword.

This approach allows you to directly map concurrent problems into concurrent constructs instead of dealing with the minutia of starting and managing threads, and mapping logic evenly across available threads. In Go, programmers model concurrency in goroutines, as concurrent functions, and channels, and communication mechanisms between parallel functions. In languages that expose threads, you need to worry about things like thread pools, and map requests onto threads. In contrast, in Go you would write a function and prepend its invocation with the go keyword. The runtime handles everything else automatically.

Goroutines

Goroutines

Channels

Channels

Memory Access and Thread Synchronization Primitives

The sync package provides memory access and thread synchronization primitives.

Memory Access Synchronization Primitives
Thread Synchronization Primitives

Context

Context

Patterns

Channel Patterns