Single Threaded Summation: Protection with Locks

This lesson explains the solution for calculating the sum of a vector problem using locks in C++.

If I protect access to the summation variable with a lock, I will get the answers to two questions.

  1. How expensive is the synchronization of a lock without contention?
  2. How fast can a lock be in the optimal case?

I can draw an interesting conclusion from question 2. If there is contention on a lock, the access time will decrease. That being said, I will only show the application of std:lock_guard.

Get hands-on with 1200+ tech skills courses.