Multithreaded Summation: Using std::lock_guard
This lesson explains the solution for calculating the sum of a vector problem using std::lock_guard in C++.
We'll cover the following
You may have already guessed that using a shared variable for the summation with four threads is not optimal; the synchronization overhead will outweigh the performance benefit. Let me show you the numbers. The questions I want to answer are still the same.
- What is the difference in performance between the summation using a lock and an atomic?
- What is the difference in performance between single threaded and multithreaded execution of
std::accumulate
?
The simplest way to make the thread-safe summation is to use a std::lock_guard
.
Using a std::lock_guard
Get hands-on with 1400+ tech skills courses.