Go Concurrency
Internal
TODO
Read or watch:
- https://go.dev/talks/2013/advconc.slide#1 + http://www.youtube.com/watch?v=QDDwwePbDtw
- https://go.dev/blog/pipelines
- https://go.dev/talks/2012/concurrency.slide#1 + https://www.youtube.com/watch?v=f6kdp27TYZs
- https://medium.com/insiderengineering/concurrent-http-requests-in-golang-best-practices-and-techniques-f667e5a19dea
- https://blogtitle.github.io/categories/concurrency/
- Rethinking Classical Concurrency Patterns https://drive.google.com/file/d/1nPdvhB0PutEJzdCq5ms6UI58dp50fcAN/view?pli=1
- https://medium.com/@souravchoudhary0306/healing-unhealthy-goroutines-in-long-running-programs-b2a2b56976e5
- Recursive Locking
- Resolve the problem of reentrant locks and come up with a valid pattern. Update Go Mutex and RWMutex | Reentrance
- https://stackoverflow.com/questions/14670979/recursive-locking-in-go
- https://rationaleemotions.com/reentrancy_not_permitted/
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 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, introduced in the language by the keyword go
. Goroutines are managed by the Go runtime, which maps them transparently onto threads. The O/S threads are not exposed in the concurrency programming model.
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, such as within a small struct
scope. Go provides an alternative concurrency programming model in the 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 introduced in the language by the chan
keyword.
This approach allows you to directly map concurrent problems onto 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 as goroutines and channels. 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.
Go's philosophy on concurrency can be summed up like this: aim for simplicity, use channels when possible, and treat goroutines as a free resource.
Goroutines
Programming Models
Go provides two main programming models for concurrency. The preferred one is based on communicating sequential processes (CSP) paradigm and encourages the use of channels. Memory access synchronization is available, but the language designers advise against using it as the first choice. This is a workflow to help selecting one model versus the other.
YES ┌──────────── Is it a performance critical section? │ │ NO │ │ │ ▼ YES │ Are you trying to transfer ownership of data? ──────────────┐ │ │ NO │ │ │ │ │ YES ▼ │ ├────── Are you trying to guard the internal state of a struct? │ │ │ NO │ │ │ │ │ NO ▼ YES │ ├────── Are you trying to coordinate multiple pieces of logic? ───────┤ │ │ ▼ ▼ Use Primitives Use Channels
Channels
Synchronization Primitives
The sync
package provides memory access and thread synchronization primitives.