Photo by Nick Morrison on Unsplash
๐๐๐ญ๐ ๐๐๐๐ and ๐๐๐๐ ๐๐จ๐ง๐๐ข๐ญ๐ข๐จ๐ง๐ฌ
๐ฅIn this post, Iโll cover key ๐๐จ๐ง๐๐ฎ๐ซ๐ซ๐๐ง๐๐ฒ concepts related to multithreaded programming in C++, which are also commonly asked in #interview_questions.
1๏ธโฃ ๐๐๐ญ๐ ๐๐๐๐๐ฌ
2๏ธโฃ ๐๐๐๐ ๐๐จ๐ง๐๐ข๐ญ๐ข๐จ๐ง๐ฌ
Before continuing, letโs understand the following concepts:
โญ There are ๐ญ๐ฐ๐จ basic approaches to concurrency:
โ๏ธ Concurrency with ๐ฆ๐ฎ๐ฅ๐ญ๐ข๐ฉ๐ฅ๐ ๐ฉ๐ซ๐จ๐๐๐ฌ๐ฌ๐๐ฌ
โ๏ธ Concurrency with ๐ฆ๐ฎ๐ฅ๐ญ๐ข๐ฉ๐ฅ๐ ๐ญ๐ก๐ซ๐๐๐๐ฌ
โญ ๐๐จ๐ง๐๐ฎ๐ซ๐ซ๐๐ง๐๐ฒ ๐ฐ๐ข๐ญ๐ก ๐๐ฎ๐ฅ๐ญ๐ข๐ฉ๐ฅ๐ ๐๐ซ๐จ๐๐๐ฌ๐ฌ๐๐ฌ:
This approach involves dividing an application into multiple ๐ฌ๐ข๐ง๐ ๐ฅ๐-๐ญ๐ก๐ซ๐๐๐๐๐ ๐ฉ๐ซ๐จ๐๐๐ฌ๐ฌ๐๐ฌ that run simultaneously, much like running a web browser and a word processor at the same time. These separate processes communicate through inter-process communication channels such as signals, sockets, files, or pipes.
โญ ๐๐จ๐ง๐๐ฎ๐ซ๐ซ๐๐ง๐๐ฒ ๐ฐ๐ข๐ญ๐ก ๐๐ฎ๐ฅ๐ญ๐ข๐ฉ๐ฅ๐ ๐๐ก๐ซ๐๐๐๐ฌ (the focus of this post):
This approach involves running multiple threads within ๐ ๐ฌ๐ข๐ง๐ ๐ฅ๐ ๐ฉ๐ซ๐จ๐๐๐ฌ๐ฌ. A process is the operating systemโs abstraction for a running program. In modern systems, a process can consist of multiple execution units, called threads, each running in the context of the process. All threads in a process share the same address space, so most data can be accessed directly by all threadsโglobal variables remain global, and pointers or references to data can be shared among threads.
โ
๐๐๐ญ๐ ๐๐๐๐๐ฌ:
A data race, as defined by the ๐++ ๐๐ต๐ข๐ฏ๐ฅ๐ข๐ณ๐ฅ, combines two key aspects:
๐ 1๏ธโฃ ๐๐ก๐๐ซ๐ข๐ง๐ : More than one thread accesses the same memory location.
๐ 2๏ธโฃ ๐๐ฎ๐ญ๐๐ญ๐ข๐จ๐ง: At least one thread writes to that memory location.
If these accesses occur simultaneously without proper synchronization primitivesโsuch as mutexes or locksโthis results in unpredictable or ๐ฎ๐ง๐๐๐๐ข๐ง๐๐ ๐๐๐ก๐๐ฏ๐ข๐จ๐ซ. Data races typically arise because threads in a concurrency model share the same memory space.
โ
๐๐๐๐ ๐๐จ๐ง๐๐ข๐ญ๐ข๐จ๐ง๐ฌ:
A race condition is ๐ ๐ฌ๐๐ฆ๐๐ง๐ญ๐ข๐ ๐๐ฅ๐๐ฐ that occurs when the ๐ญ๐ข๐ฆ๐ข๐ง๐ or ๐จ๐ซ๐๐๐ซ of events affects the correctness of a section of code. In concurrency, a race condition arises when the outcome depends on the relative ordering of operations across two or more threadsโthe threads "๐ซ๐๐๐" to perform their operations. Race conditions are challenging to detect because they depend on ๐ญ๐ก๐ ๐ข๐ง๐ญ๐๐ซ๐ฅ๐๐๐ฏ๐ข๐ง๐ of thread executions.
โก๏ธ ๐๐;๐๐:
Data races concern access to the same memory location, while race conditions concern the correctness of code execution.
โก๏ธ ๐๐๐ฌ๐จ๐ฎ๐ซ๐๐๐ฌ:
1๏ธโฃ API Design for C++ (2nd Edition) by Martin Reddy
2๏ธโฃ C++ Concurrency in Action: Practical Multithreading (2nd Edition) by Anthony Williams
3๏ธโฃ Concurrency with Modern C++ by Rainer Grimm
4๏ธโฃ Modern Operating Systems (5th Edition) by Andrew S. Tanenbaum and Herbert Bos
I hope you enjoy reading it! ๐ธ
#cpp #multithreading #concurrency #cplusplus #learncpp #IloveCpp ๐ #programming #code #softwaredevelopment #codinglife #moderncpp #templateprogramming #cplusplus17 #cplusplus20 #cppdeveloper #cppcommunity