Go Concurrency: Difference between revisions
Line 5: | Line 5: | ||
=Overview= | =Overview= | ||
The majority of languages address concurrency by providing a representation of [[Concurrent_(Parallel)_Programming#O/S_Threads|O/S threads]] or [[Concurrent_(Parallel)_Programming#Green_Threads|green threads]] at language level, and exposing memory access synchronization primitives to protect data in presence of concurrent access. | The majority of languages address concurrency by providing a representation of [[Concurrent_(Parallel)_Programming#O/S_Threads|O/S threads]] or [[Concurrent_(Parallel)_Programming#Green_Threads|green threads]] at language level, and exposing [[Concurrent_(Parallel)_Programming#Memory_Access_Synchronization_Primitives|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 keyword <code>go</code>. Goroutines are managed by the Go runtime, which maps them transparently over threads. While Go provides memory access and thread synchronization primitives at the language level via the <code>sync</code> package, the language guidelines do not exactly encourage their use, unless in very specific situations. Go provides an alternative concurrency programming model in language, based on Communicating Sequential Processes (CSP), a model introduced by A. Hoare in the "Communicating Sequential Processes" 1978 ACM paper. This concurrency programming model is centered on channels, which are exposed at language level with another keyword (<code>channel</code>). | Go takes a different approach. The fundamental concurrent execution primitive is the goroutine, exposed by the language with the keyword <code>go</code>. Goroutines are managed by the Go runtime, which maps them transparently over threads. While Go provides memory access and thread synchronization primitives at the language level via the <code>sync</code> package, the language guidelines do not exactly encourage their use, unless in very specific situations. Go provides an alternative concurrency programming model in language, based on Communicating Sequential Processes (CSP), a model introduced by A. Hoare in the "Communicating Sequential Processes" 1978 ACM paper. This concurrency programming model is centered on channels, which are exposed at language level with another keyword (<code>channel</code>). |
Revision as of 23:09, 2 January 2024
Internal
Overview
The majority of languages address concurrency by providing a representation of O/S threads or green threads at language level, and exposing 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 keyword go
. Goroutines are managed by the Go runtime, which maps them transparently over threads. While Go provides memory access and thread synchronization primitives at the language level via the sync
package, the language guidelines do not exactly encourage their use, unless in very specific situations. Go provides an alternative concurrency programming model in language, based on Communicating Sequential Processes (CSP), a model introduced by A. Hoare in the "Communicating Sequential Processes" 1978 ACM paper. This concurrency programming model is centered on channels, which are exposed at language level with another keyword (channel
).
Goroutines
OS threads.
Logically, a goroutine is a function executing concurrently with many other functions in the program. Internally, the goroutine is a structure managed by the Go runtime that with a very small initial memory footprint (a few kilobytes) and that is managed by the runtime, which grows or shrinks the memory allocated for the stack automatically. The CPU overhead averages about three instructions per function call. It is practically al to create hundreds of thousands of goroutines in the same address space.
DEPLETE and DELETE Go_Language_Goroutines
Channels
CSP.