← Back to blog

The World of Concurrency in C++

·1 min read

C&C++

This is an early study note from 2020. Some of it may be out of date.

Main topics:

  • What are concurrency and multithreading?
  • Why use concurrency and multithreading?
  • How do we use concurrency and multithreading?

What is concurrency?

Two or more independent activities happening at the same time. In computing, this means: a single system performing multiple independent tasks at the same time.

Task switching on a single core vs. hardware concurrency on multiple cores

  • Task switching on a single core

Single-core task switching

  • Hardware concurrency on multiple cores

Multi-core hardware concurrency

Concurrency with multiple processes

Separate processes can pass messages to each other through the usual inter-process communication channels (signals, sockets, files, pipes and so on).

One drawback: the fixed overhead of running a process. It takes time to start a process, and the operating system needs internal resources to manage it.

An extra advantage of using separate processes: you can run them on different machines connected over a network.

Concurrency with multiple threads

Each thread runs independently, and all threads share the same address space, so threads can communicate through shared memory.

If data is accessed by multiple threads, you must make sure that the data each thread sees is consistent.