Concurrency¶
Category: Foundations · Status: stub · Lessons: chapter 01, Threads
One line: Structuring a program as tasks whose lifetimes overlap, so that all of them make progress over the same period of time — interleaved on one core, or at the same instant on several.
Also called: concurrent programming, concurrent computing.
How it connects¶
flowchart LR
n_asynchrony["Asynchrony"]
n_concurrency["Concurrency"]
n_distributed_computing["Distributed computing"]
n_parallelism["Parallelism"]
n_sequential_execution["Sequential execution"]
n_asynchrony ---|vs| n_concurrency
n_concurrency ---|vs| n_distributed_computing
n_concurrency ---|vs| n_parallelism
n_concurrency ---|vs| n_sequential_execution
classDef center stroke-width:3px
class n_concurrency center
classDef outside stroke-dasharray: 4 3
class n_asynchrony,n_distributed_computing,n_parallelism,n_sequential_execution outside
- Often confused with: Asynchrony, Distributed computing, Parallelism, Sequential execution
- See also: Concurrency models, Interleaving, Multitasking
In each language¶
| Rust | The book's Fearless Concurrency ↗ chapter: threads, message passing and shared state, with ownership and type checking turning many concurrency errors into compile-time errors |
| Go | In the language: the go statement ↗ starts a goroutine, and channels ↗ and select ↗ connect goroutines |
| C | C11's <threads.h> ↗ is optional (a compiler may define __STDC_NO_THREADS__); POSIX threads start with pthread_create ↗ |
| C++ | The concurrency support library ↗ since C++11: threads, mutexes, condition variables and futures, with std::jthread added in C++20 |
| Java | Thread ↗ since 1.0, and the executors, locks and concurrent collections of java.util.concurrent ↗ |
| Python | The library's Concurrent Execution ↗ chapter (threading, multiprocessing, concurrent.futures), and asyncio ↗ for async/await code |
| C# | Task-based asynchronous programming ↗ with Task, over managed threads ↗ |
| JavaScript | Each agent, analogous to a thread, runs an event loop ↗: concurrency comes from promises and async functions, parallelism from workers ↗ |
| Kotlin | Coroutines ↗: the language has suspend, and async, launch and the rest come from the kotlinx.coroutines library |
| Swift | In the language: async/await, tasks and actors ↗ |
| Erlang and Elixir | Lightweight processes ↗, fast to create and terminate, that communicate by sending messages |
| Haskell | forkIO ↗ threads scheduled by the GHC runtime, communicating through MVars |
Where to read more¶
- In this library: Who waits when main returns?
- In the books: Concurrency in Go, Katherine Cox-Buday — ch. 1, 'An Introduction to Concurrency'
- In the books: C++ Concurrency in Action, Anthony Williams — ch. 1, 'Hello, world of concurrency in C++!'
- In the books: Programming Concurrency on the JVM, Venkat Subramaniam — ch. 1, 'The Power and Perils of Concurrency'
- In the books: Concurrency in C# Cookbook, Stephen Cleary — ch. 1, 'Concurrency: An Overview'
- In the books: Grokking Concurrency, Kirill Bobrov — ch. 1, 'Introducing concurrency'
- In the books: Learning Concurrent Programming in Scala, Aleksandar Prokopec — ch. 1, 'Introduction' → 'Concurrent programming'
- Notes: Concurrency - concurrent operations ↗
- Notes: concurrently - rust - main - concurrency ↗
- Notes: Concurrency vs Parallelism - concurrent vs parallel execution ↗
- Notes: Asynchronous - Concurrency - Parallelism - general concept ↗
- Notes: async vs concurrent ↗
- Notes: Index - Concurrency and Parallelism in general - main ↗
- Reference: Wikipedia: Concurrent computing ↗