Member-only story
C++ Multithreading from Scratch — Part 4
One of the greatest benefits of multithreading is the ability to share data directly between threads; this reduces resource consumption and improves program performance.
However, sharing the same memory space comes at a cost. To understand this better, imagine a restaurant kitchen:
A waiter writes a complete order on a ticket and leaves it on the counter. In this kitchen, there are several chefs who will divide the dishes on the ticket among themselves to prepare them faster. Here, the chefs are the threads and the order ticket is the data.
If there is no organizational system, chaos will break out at any moment:
- First chaos scenario: A chef sees the ticket, decides to prepare the steak, and gets to work on it, but forgets to cross it off the list. The next chef who approaches the counter sees the ticket, assumes no one is making the steak, and starts preparing another one. In the end, we will have two steaks ready and a customer waiting for their salad. In programming, we call this a race condition.










