Introduction to Concurrency
Explore the fundamentals of concurrency in C++. Understand the difference between concurrency and parallelism, how processes and threads function, and why concurrency improves responsiveness and resource use. Learn about data races and synchronization to write safe concurrent code.
Modern processors offer significant parallel processing capabilities, often featuring eight, sixteen, or even more cores. However, up to this point, the programs we have written have executed sequentially on a single core, processing one instruction at a time. To develop high-performance software that remains responsive and handles large workloads efficiently, we must move beyond this sequential model and embrace concurrent execution.
In this lesson, we will establish the conceptual foundations of multitasking. We will examine how modern hardware supports parallel operations and explore why naïvely attempting to “run everything at once” introduces a new set of challenges that must be addressed deliberately and carefully
Concurrency vs. parallelism
In C++, the terms concurrency and parallelism are often used interchangeably, but they describe distinct concepts.
Concurrency is concerned with program structure. It refers to organizing a program as multiple independent tasks that can make progress out of order or with partial overlap. A concurrent program is designed to manage many activities at once, even if they are not executing simultaneously.
Parallelism is concerned with execution. It refers to the literal, simultaneous execution of multiple tasks. A parallel program performs multiple operations at the same time.
It is possible to have concurrency without parallelism. On a single-core processor, the operating system provides concurrency through context switching, rapidly pausing one task and resuming another to create the illusion of simultaneous execution. It is ...