Modern C++ Concurrency - Synchronizing threads - Condition Variables
Introduction
In the previous mutex
t1
This lesson discusses the tools that we can use to achieve such behavior efficiently condition variables
In the previous mutex
t1
This lesson discusses the tools that we can use to achieve such behavior efficiently condition variables
In this lesson, we will cover the topic of data sharing and resources between threads.
Imagine a scenario where an integer o
needs to be modified by two threads t1
and t2
.
If we are not careful in handling this scenario a data race might occur. But what is a data race exactly?
A data race occurs when two or more threads access some shared data and at least one of them is modifying such data. Because the threads are scheduled by OS, and scheduling is not under our control, you do not know upfront which thread is going to access the data first. The final result might depend on the order in which threads are scheduled by the OS.
Race conditions occur typically when an operation, in order to be completed, requires multiple steps or sub-operations, or the modification of multiple data. Since this sub-operations end up being executed by the CPU in different instructions, other threads can potentially mess up with the state of the data while the other's thread operation is still ongoing.
Read More »Modern C++ Concurrency - How to share data and resources between threads