Leanpub Header

Skip to main content

Topics

Concurrency

From threads, locks and lock-free structures to event loops, coroutines, actors and parallel computing on many cores, concurrency as told through the free samples of Leanpub books.

Created from the free samples of 12 Leanpub books and the descriptions and tables of contents of 18 more, with every source cited.

Concurrency is the structuring of a program as several flows of execution whose relative timing the program does not fix. Parallelism is something narrower: the simultaneous execution of several computations on separate processor cores. A program can be concurrent without being parallel, as when a single thread attends to many network connections in turn, and the sources insist that the two words are not synonyms.[1] [6]

The subject became unavoidable in the mid-2000s, when processor clock speeds stopped rising and manufacturers added cores instead, so that a single-threaded program uses one core and leaves the rest idle.[1] Several styles of programming have grown up around it: threads that share memory under locks, lock-free structures built on atomic operations, event loops and asynchronous functions, coroutines, processes that exchange messages, and libraries that spread one computation over many cores or a graphics processor. A single language may now offer most of them at once.[2]

This article describes those styles and the errors particular to them. Its sources are the free samples of 12 Leanpub books and the public descriptions and tables of contents of 18 more. Between them they cover C++, Kotlin, Python, JavaScript, Rust, C#, Java, PHP, Delphi and Elixir, and most were published between 2024 and 2026.

Where to start

A reader who wants the ideas before any one language has two short entries. Asynchronous programming (2021) sets out to explain asynchronous code from low-level socket operations upward, for developers who want to put in order what they already half know.[8] Java Concurrency Wars: The Conquest Against Deadlocks (2026) is a comic-style introduction in which five colleagues investigate a production incident and meet threads, race conditions, locks and deadlocks on the way.[9] The first chapter of The Art of Concurrent Programming (2026) defines the problem carefully before the book turns to hardware, though the book as a whole is written for experienced engineers.[1]

Most readers will start from the language they work in. For C++ there is Concurrency with Modern C++ (2024), addressed to professional C++ programmers; for Kotlin, Kotlin Coroutines (2026), which assumes the reader knows the language well; for Python, Asyncio from Groundup (2026), which builds the runtime first and introduces the keywords afterwards; for JavaScript on the server, Node.js Event Loop (2025), for intermediate developers; and for Rust, Asynchronous Programming in Rust (2024), which works through examples up to a runtime of the reader's own.[2] [3] [17] [18] [19]

Two books work at the level of the system. Libuv Mastery (2026) teaches event-driven network programming to C programmers and requires no previous experience of asynchronous code, and Lock-Free C++ in Production (2026) is for C++ engineers who need lock-free code to be correct on every processor architecture it is shipped to.[4] [16]

History

Much of the vocabulary is older than the hardware that made it urgent. The actor model, in which independent entities compute, create other actors and exchange messages, was defined in 1973. In 1986 a Swedish telecommunications company began work on Erlang, a language for telephone switches that had to run for years without stopping, whose processes share no memory and communicate only by messages.[10] The classic puzzle of contention for shared resources, five philosophers at a table who each need two forks to eat, is still used as a case study.[2]

Two pressures brought the subject to ordinary programmers. One was the network. A web page of 1999 posed the problem of serving ten thousand simultaneous connections on commodity hardware, at a time when the usual design gave each connection a thread of its own, with a megabyte or more of stack apiece and heavy switching costs. The answer was the event-driven server, in which one thread watches many connections, and a conference talk of 2009 that paired a JavaScript engine with such an event loop produced Node.js and spread the style to most languages.[4] The other pressure was the processor. Until the mid-2000s programs became faster without being changed; when clock speeds met their physical limit, manufacturers multiplied cores, and where a desktop of 2005 had one, a workstation of 2025 might have sixty-four.[1]

Languages followed. C++ gained a standard threading interface and a memory model in 2011, parallel versions of its standard algorithms in 2017 and coroutines in 2020.[2] Early Java ran all its threads inside the virtual machine on a single operating-system thread, which made them cheap but meant that one blocking call stopped the whole process; later runtimes, Go's among them, schedule many lightweight threads across several kernel threads.[1]

Concurrency and parallelism

The Art of Concurrent Programming gives the distinction in two sentences. Parallelism is a property of hardware execution, several operations advancing together in real time; concurrency is a structural property of a program that manages independent flows which may run in parallel, interleave on one core or both, and which must be correct however they interleave.[1] Its examples of concurrency without parallelism are a single-threaded event loop serving many clients and a shell pipeline, whose processes interact through buffers with no fixed order between them. It concludes that concurrency is a design problem and parallelism an implementation detail.[1]

The other axis is whether a call waits. In a synchronous program each task blocks everything until it has finished; in an asynchronous one the program can switch to other work while a task is pending, and this can be done on a single thread.[6] Async PHP: Early Access (2017) describes it as a cooperative arrangement in which the order of execution is no longer predictable, and reserves the word parallel for work spread over several threads or processes.[7] The reason to bother is that processors are orders of magnitude faster than networks, so that a program which spends its time on input and output, called I/O-bound, leaves the processor idle while it waits.[8]

What makes the subject hard is the nature of its bugs. A sequential bug fails the same way every time. A data race between two threads may show itself once in a thousand runs or once in a million, never on a developer's four cores and constantly on a production server's sixty-four, and may vanish when logging is added because the logging changes the timing.[1] A deadlock, in which two threads each hold one lock and wait for the other's, may need load to appear. False sharing, where two threads write to different variables that happen to share a cache line, produces no wrong result at all, only a program that can run ten times slower than it should.[1]

The same book frames every later choice as a trade-off. A single lock around all shared state is easy to get right and lets only one thread proceed; finer locking and lock-free designs scale better and are easier to get wrong. High-level abstractions such as actors, channels and asynchronous functions hide detail but may not map efficiently to the hardware, and code that leans on the strong memory ordering of one processor family may fail on another.[1]

Threads and shared memory

A process is a running program with its own address space, isolated from other processes, and it is costly to create. A thread is a unit of execution inside a process that shares the address space with its sibling threads, so that they can read and write one another's variables directly.[1] Kernel threads are scheduled by the operating system and can run on different cores at once; user threads are scheduled by a language runtime on top of a smaller number of kernel threads and are far cheaper to create and to switch between.[1]

Sharing is where the danger lies. In C++, reading and writing a shared variable from two threads without coordination is a data race and the behaviour of the program is undefined.[2] The basic remedy is the mutex, which lets one thread at a time into the critical section that uses the variable, wrapped in a lock object that releases it automatically. The standard library adds condition variables, by which one thread signals another and which are hard to use correctly, and, since 2020, semaphores, latches and barriers.[2] A higher abstraction is the task, a job handed to the runtime whose result travels from a promise at one end to a future at the other.[2]

The author of Parallel Programming with OmniThreadLibrary (2019), a guide to a threading library for Delphi by the library's main developer, takes a side: locking leads to slow code and to deadlocks, and is a main reason for code that almost works. The library is organised around tasks instead of threads, and wherever possible it replaces shared data with messages between a task and its owner, at the cost of more memory and copying.[5] Its high-level layer offers ready-made patterns such as futures and pipelines, so that the programmer writes the worker code and the library supplies the threading.[5]

Other books cover the same ground for their platforms. Multithreading and Parallel Programming in C# (2019), a course, moves through threads, the thread pool, task-based parallelism, asynchronous functions, synchronisation and concurrent collections in C#.[21] Asynchronous Programming with C++ (2025) does so for C++, from threads, locks and atomic operations to promises, futures and coroutines.[20] Java Concurrency Wars: The Conquest Against Deadlocks follows its deadlock with debugging from thread dumps and with the choice of the right concurrency tool.[9]

Atomics and lock-free programming

Beneath locks are atomic operations, which complete without interruption, and the memory model, which states when one thread's writes become visible to another. C++ lets a programmer relax the default guarantee of a single global order of operations in exchange for speed.[2] Lock-free data structures are built from these parts. The Art of Concurrent Programming credits them with better worst-case latency than locks, because a stalled thread does not hold up the others, and with possibly worse average latency when contention is low.[1]

Lock-Free C++ in Production begins with a queue that passed every test for a year and then returned corrupted data on servers of a different processor architecture, because the first had quietly supplied orderings the code never asked for. The book covers queues, memory reclamation schemes and verification, and its first chapter includes the cases where lock-free is the wrong answer.[16] Parallel Programming with OmniThreadLibrary adds a caution about the term. Its own lock-free queues avoid the operating system's locks but rely on a processor instruction that locks the memory bus, which is slower than ordinary code and faster than a system lock.[5] Deterministic Matching Engines in C++23/26 (2026) applies these techniques to the order book of a trading system.[28]

Event loops and asynchronous programming

An event loop is the alternative to a thread per task. A blocking read stops the calling thread until data arrives; a non-blocking one returns at once, and the program does other work until the operating system reports that the connection is ready. Each connection then needs only a descriptor, some buffers and a function to call, and Libuv Mastery puts ten thousand connections at a few megabytes in this style against about ten gigabytes of thread stacks in the older one.[4] A program built this way needs no locks, since only one thing runs at a time, but it keeps the other problems of concurrency: operations from different logical flows still interleave.[1]

JavaScript is the best-known case. Its engine runs on one thread, and an event loop moves completed callbacks from a queue onto the call stack whenever the stack is empty.[6] libuv is the C library that provides the loop beneath Node.js, and Node.js Event Loop is about what runs in each phase of that loop and how to keep slow code from blocking it.[4] [18] Asynchronous functional javascript (2025) treats callbacks, promises and asynchronous functions with the vocabulary of functional programming, to which it says it is not an introduction, and Generators in JavaScript covers generator functions and their use in asynchronous code.[6] [24]

Python's standard asynchronous library has three books. Asyncio from Groundup argues that the topic confuses people because it is taught keywords first, and has the reader write a small event loop by hand.[17] Advanced asyncio, Practically Python (2026) is about what tutorials leave out, a stalled supplier, a full queue, a client that disconnects, a deployment that cancels a thousand tasks in flight, and grows one small service through each.[14] Async Patterns in Django (2024) applies the style to a web framework, with task queues, WebSockets and streaming responses, and includes a section on when not to use it.[25]

The pattern reaches other languages in the same form. Async PHP: Early Access, an early-access book that was not finished, argues that PHP, whose scripts were designed to live for one request, can serve persistent connections with the right extensions.[7] Asynchronous Programming in Rust explains the operating-system mechanisms first and then Rust's futures.[19] C# & AI Masterclass: Asynchronous AI Pipelines. Async/Await, Parallelism, and Streaming LLM Responses. (2026) applies asynchronous streams, channels, throttling and cancellation in C# to programs that call language models.[26]

Coroutines and structured concurrency

A coroutine is a function that can suspend its execution and resume later with its state intact. C++ gained a framework for writing coroutines in 2020 and its first ready-made one, a generator, in 2023.[2] In Kotlin they are the main tool for asynchronous work. Kotlin Coroutines, which grew out of a workshop, explains first how suspension works and then the standard library built on it. The rule it states is that a coroutine should suspend and never block its thread: a delay sets a timer and frees the thread for other coroutines.[3]

The same book explains structured concurrency. A coroutine started within a scope becomes a child of that scope, which gives every coroutine a parent. A parent cannot complete until its children have, cancelling a parent cancels its children, and an exception in a child is passed to the parent. Starting coroutines on the global scope breaks those relationships, and the book calls it bad practice.[3] Advanced asyncio, Practically Python brings the same idea to Python under the maxim that every task needs a named owner.[14]

Master Kotlin Coroutines & Flow for Android (2026) teaches coroutines to Android developers, starting from the platform's threading problem and from raw threads and callbacks as failed first attempts.[23] Kotlin Coroutines (2024) is a course by the author of Kotlin Coroutines with the same outline and added exercises.[22]

Message passing and actors

The alternative to sharing memory is to share nothing. Each Erlang process has its own heap and stack and communicates only through asynchronous messages, which removes data races by construction, and the processes are so light, a few kilobytes each, that one machine can run millions of them. The Professional’s Guide to Elixir (2026) gives this history as the foundation of Elixir, a newer language on the same virtual machine.[10]

The idea appears in other languages as channels and actors. Go programs coordinate lightweight threads through channels, and Kotlin's coroutine library has channels of its own.[1] [3] Rust Projects - Write a Redis Clone (2026), a project book that builds a clone of an in-memory database in Rust, ends with the actor model as its way of handling many client connections.[27]

Parallel computing

Where the aim is speed and not responsiveness, the unit of work is data. Since 2017 most algorithms of the C++ standard library can be called with an execution policy that asks for sequential, parallel or parallel and vectorised execution.[2] Advanced Topics in Parallel and Distributed Systems Design (2021) begins from the textbook classification of parallel machines and the programming models that go with them, shared memory, threads, message passing and data parallelism.[15]

Parallel Python with Dask (2026) is a guide to Dask, a Python library that parallelises work written for the language's usual data tools without requiring the programmer to manage the low-level detail. Its ten chapters run from the library's architecture and collections to its use with data frames, machine learning libraries and graphics processors.[11]

Graphics processors are the far end of the scale. GPU Parallel Computing (2026) is an introduction for readers new to their architecture, from how they execute threads and manage memory to the two main programming interfaces, synchronisation and the use of several processors together.[29] GPU Programming using Rust and CUDA (2026) tries three toolchains that connect Rust to one vendor's hardware, growing a single project from device queries to matrix multiplication and checking every result against a reference computed on the central processor.[30]

Databases and distributed systems

Concurrency does not stop at the edge of a program. PostgreSQL Under Load: Connections, Transactions, MVCC and Concurrency (2026) is about a database under many simultaneous clients: why PostgreSQL uses a process for each connection instead of a thread, how connection pooling, snapshots of the data for each transaction, locks and isolation levels interact, and how an exhausted connection pool can cascade into lock contention.[13]

Surviving Distributed Systems: A Practical Field Guide (2026) carries the subject across services. Its premise is that most production incidents come from code written as if a remote call behaved like a local one, and its examples include a retry loop that turns a partial failure downstream into a multiplied load. The examples are written in Java, and the book says the failure modes lie in sockets, database transactions and operating-system schedulers and so apply to any runtime.[12]

Questions about Concurrency

What is the difference between concurrency and parallelism?

Parallelism is several computations actually running at the same time on separate cores, a property of hardware execution.[1] Concurrency is a property of a program's structure: it manages several independent flows that may run in parallel or interleave on one core, and it must be correct either way.[1]

What is a data race?

A data race is two threads reading and writing the same shared variable without coordination, which in C++ makes the program's behaviour undefined.[2] Such a bug may appear once in a thousand runs or only under production load, and may vanish when logging changes the timing.[1]

What is the C10K problem?

It is the problem, posed in 1999, of serving ten thousand simultaneous connections on commodity hardware.[4] A thread for each connection cost too much memory and switching time, and the answer was the event-driven server, in which one thread watches many connections.[4]

What is structured concurrency?

In structured concurrency every coroutine is started within a scope and becomes a child of it.[3] A parent cannot complete until its children have, cancelling the parent cancels them, and a child's exception is passed to the parent.[3]

Is lock-free code always faster than code that uses locks?

No. Lock-free structures tend to give better worst-case latency, because a stalled thread does not block the others, but may have worse average latency when contention is low.[1] They are also harder to get right, and the first chapter of Lock-Free C++ in Production covers when lock-free is the wrong answer.[16]

Which concurrency book should I start with?

For the ideas without a particular language, Asynchronous programming (2021) explains asynchronous code from socket operations upward.[8] Otherwise start from your language: Concurrency with Modern C++ (2024) for C++, Kotlin Coroutines (2026) for Kotlin, or Asyncio from Groundup (2026) for Python.[2] [3] [17]

Sources

Every source is a Leanpub book or course. The note after each says which part of it was read; where no sample was available, only the public description and table of contents were used.

  1. 1.
    The Art of Concurrent Programming, Steve Publications: Locks, Atomics, Lock-Free Systems and High-Performance Concurrent Software Free sample: chapter 1 on concurrency and parallelism in full, and the start of chapter 2 on processes, threads and context switching. 2026. Free sample: PDF · EPUB
  2. 2.
    Concurrency with Modern C++, Rainer Grimm: What every professional C++ programmer should know about concurrency. Free sample: the overview chapter, a summary of the whole book from the memory model to coroutines and the case studies. 2024. Free sample: PDF · EPUB
  3. 3.
    Kotlin Coroutines, Marcin Moskała: Deep Dive Free sample: the introduction and the chapter on coroutine builders and structured concurrency; the chapters of the first part appear as headings only. 2026. Free sample: PDF · EPUB
  4. 4.
    Libuv Mastery, Steve Publications: Building High-Performance Asynchronous Applications in C Free sample: the introduction and the start of chapter 1, on the problem of ten thousand connections and the concurrency models compared. 2026. Free sample: PDF · EPUB
  5. 5.
    Parallel Programming with OmniThreadLibrary, Primož Gabrijelčič Free sample: the introduction to the library, with its sections on tasks and threads and on locking and messaging. 2019. Free sample: PDF · EPUB
  6. 6.
    Asynchronous functional javascript, dimitris papadimitriou Free sample: the purpose of the book and the opening chapter on concurrency, parallelism, asynchrony and the event loop. 2025. Free sample: PDF
  7. 7.
    Async PHP: Early Access, Christopher Pitt: Rethinking Traditional PHP Free sample: the preface and introduction, with the section that clarifies synchronous, asynchronous and parallel execution. An early-access book that was not finished. 2017. Free sample: PDF
  8. 8.
    Asynchronous programming, Kirill Bobrov Free sample: the opening page of chapter 1, on blocking and non-blocking input and output. 2021. Free sample: PDF
  9. 9.
    Java Concurrency Wars: The Conquest Against Deadlocks, Akshat Sharma Free sample: the cast, the prologue and the road map of fourteen missions. 2026. Free sample: PDF
  10. 10.
    The Professional’s Guide to Elixir, Steve Publications: A Comprehensive Guide to Functional Programming, Concurrency, and Fault-Tolerant Systems Free sample: the introduction and the start of chapter 1, on the origins of Erlang and its process model. 2026. Free sample: PDF · EPUB
  11. 11.
    Parallel Python with Dask, GitforGits | Asian Publishing House: Make code reusable and deployed for high performance web apps Free sample: the prologue, the table of contents and the preface. 2026. Free sample: PDF
  12. 12.
    Surviving Distributed Systems: A Practical Field Guide, Artur Ostapyshyn: A Practical Field Guide to Resilience, Concurrency, and Fault Tolerance Free sample: the contents, the section about the book and part of a code listing from chapter 1. 2026. Free sample: PDF · EPUB
  13. 13.
    PostgreSQL Under Load: Connections, Transactions, MVCC and Concurrency, Steve Publications: A definitive guide to operating PostgreSQL reliably under real-world production workloads. The sample holds headings only: from the description and table of contents. 2026. Free sample: PDF · EPUB
  14. 14.
    Advanced asyncio, Practically Python, Nix: Reliable concurrency in modern Python — structured concurrency, cancellation, backpressure and graceful shutdown The sample has no readable text: from the description. 2026. Free sample: PDF
  15. 15.
    Advanced Topics in Parallel and Distributed Systems Design, Joseph Thachil George: Parallel and Distributed Systems Design The sample holds the table of contents only: from it and the description. 2021. Free sample: PDF
  16. 16.
    Lock-Free C++ in Production, Weston Ashgrove: Mastering the Memory Model with Hazard Pointers and Epoch-Based Reclamation No sample: from the description and table of contents. 2026.
  17. 17.
    Asyncio from Groundup, Ritesh Modi: A working Mental Model for Python asyncio No sample: from the description. 2026.
  18. 18.
    Node.js Event Loop, A. Jobaer: Mastering Concurrency with Asynchronous Patterns and Non-Blocking Design in Node.js No sample: from the description and table of contents. 2025.
  19. 19.
    Asynchronous Programming in Rust, Packt Publishing Ltd: Learn asynchronous programming by building working examples of futures, green threads, and runtimes No sample: from the description and table of contents. 2024.
  20. 20.
    Asynchronous Programming with C++, Packt Publishing Ltd: Build blazing-fast software with multithreading and asynchronous programming for ultimate efficiency No sample: from the description and table of contents. 2025.
  21. 21.
    Multithreading and Parallel Programming in C#Course, EngineerSpock: Overcome multithreading and asynchronous programming in C# problems & improve performance by parallel computing in C#. No sample: from the description and table of contents. 2019.
  22. 22.
    Kotlin CoroutinesCourse, Marcin Moskała No sample: from the description and table of contents. 2024.
  23. 23.
    Master Kotlin Coroutines & Flow for Android, Mahmoud Ramadan: Build Fast, Responsive, and Production-Ready Android Apps with Kotlin Coroutines, Flow, StateFlow, SharedFlow, Channels, Jetpack Compose, ViewModel, Room, Retrofit, and Modern Android Architecture No sample: from the description and table of contents. 2026.
  24. 24.
    Generators in JavaScript, Parthipan Natkunam: A Guide to Building Robust Asynchronous Logic for Your Applications No sample: from the description and table of contents.
  25. 25.
    Async Patterns in Django, Paul Bailey: A comprehensive guide to asynchronous programming and protocols in Django No sample: from the description and table of contents. 2024.
  26. 27.
    Rust Projects - Write a Redis Clone, Leonardo Giordani: Explore asynchronous programming with the actor model using Rust and Tokio No sample: from the description. 2026.
  27. 28.
    Deterministic Matching Engines in C++23/26, Weston Ashgrove: Lock-Free Order Books, Cache-Line Aligned Price-Time Priority, and Zero-Jitter Trade Execution. No sample: from the description. 2026.
  28. 29.
    GPU Parallel Computing, gareth thomas: From Basics to Breakthroughs in GPU Programming No sample: from the description and table of contents. 2026.
  29. 30.
    GPU Programming using Rust and CUDA, GitforGits | Asian Publishing House: Exploring Rust’s potential in GPU and parallel computing using Rust-CUDA, cuda-oxide, and RustaCUDA No sample: from the description. 2026.

Further reading

Other books in the library on the same ground, not cited above.

  • Programming with C++20, Andreas Fertig: Concepts, Coroutines, Ranges, and more The new features of C++20 for programmers who know C++11, with one chapter on coroutines. 2026. Free sample: PDF
  • Modern C++23 QuickStart Pro, GitforGits | Asian Publishing House: Advanced programming including variadic templates, lambdas, async IO, multithreading and thread sync A C++23 book for experienced developers, with chapters on multithreading, atomics, thread pools and lock-free data structures. 2026.
  • C++ Programming Cookbook, GitforGits | Asian Publishing House: Proven solutions using C++ 20 across functions, file I/O, streams, memory management, STL, concurrency, type manipulation and error debugging A recipe-style C++20 book with one chapter on concurrency. 2026. Free sample: PDF
  • Rust In Practice, Third Edition, GitforGits | Asian Publishing House: Concurrency, Memory Safety, GPU Computing, Linux Kernels, and Real-World Systems using Rust 1.85 A general Rust book that grows one system tool chapter by chapter, with a chapter on asynchronous code. 2026.
  • Kotlin Design Patterns and Best Practices - Third Edition, Packt Publishing Ltd: Elevate your Kotlin skills with classical and modern design patterns, coroutines, and microservices Design patterns in Kotlin, with chapters on reactive and concurrent patterns. 2024.
  • Master Kotlin Multiplatform, Mahmoud Ramadan: Build Production-Ready Android and iOS Apps with Kotlin Multiplatform, Compose Multiplatform, Ktor, Coroutines, and Modern Architecture Building one mobile app for two platforms in Kotlin, with a chapter on concurrency. 2026.
  • Architecting High Frequency Trading Systems, Weston Ashgrove: Modern C++ design patterns for Lock-Free Pipelines, Kernel Bypass, and Nanosecond Execution. Low-latency trading infrastructure in C++, including lock-free queues. 2026.
  • Deterministic Risk Engines in C++, Weston Ashgrove: Lock-Free VaR Computation, Real-Time Stress Testing, and Cache-Aligned Position Tracking for High-Frequency Trading. A real-time risk engine for trading in C++, with parallel stress testing. 2026.
  • Android Internals 2: Performance, Concurrency, and Runtime, Abdur-Rahmaan Janhangeer Android performance and runtime internals, with one chapter on concurrency. 2026.
  • Software That Lasts, Kaj Bromose: Professional C# Development with Clean Architecture, DDD, and Concurrency A C# book on code quality and architecture with a part on concurrency and parallelism. 2026.

This is an article created from the free samples of 12 Leanpub books and the descriptions and tables of contents of 18 more. We're building these articles to provide you, the Leanpub reader, with a good jumping-off point to discover more Leanpub books that are interesting and relevant to you. We never use material which is paid-only when building these articles: we only use material which is already free on Leanpub. However, Leanpub authors can opt out from inclusion in articles from their book settings page.

Updated November 2026.

All topics