๐ƒ๐š๐ญ๐š ๐‘๐š๐œ๐ž and ๐‘๐š๐œ๐ž ๐‚๐จ๐ง๐๐ข๐ญ๐ข๐จ๐ง๐ฌ

ยท

2 min read

๐Ÿ”ฅ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

ย