Skip to content

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

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