Protect Shared Changes
Protect Shared Changes
A mutex protects a shared operation from overlapping accesses.
A lock_guard acquires the lock for its scope and releases it automatically, letting two attendance workers update one count safely.
Before this lesson, review Run a Task in Another Thread when needed.
What you’ll learn
- Use mutexes to protect Shared Changes.
- Explain how the complete example produces 2.
- Make the practice change and explain why the result changes or stays the same.
Follow a complete C++ example
A lock_guard acquires the lock for its scope and releases it automatically, letting two attendance workers update one count safely.
Read the declarations above main before tracing its statements. Each declared name supplies the value, type or operation used later. The std:: prefix selects a name from the standard library, and cout << sends a value to the output stream.
C++17 · main.cpp · complete program
#include <iostream>
#include <thread>
#include <mutex>
int main() {
int count = 0;
std::mutex mutex;
auto add = [&] {
std::lock_guard<std::mutex> lock(mutex);
++count;
}
;
std::thread first(add);
std::thread second(add);
first.join();
second.join();
std::cout << count << '\n';
return 0;
}
Expected output
2Run in C++ compiler →Connect the code to its result
C++17 · source used in this explanation
#include <iostream>
#include <thread>
#include <mutex>
int main() {
int count = 0;
std::mutex mutex;
auto add = [&] {
std::lock_guard<std::mutex> lock(mutex);
++count;
}
;
std::thread first(add);
std::thread second(add);
first.join();
second.join();
std::cout << count << '\n';
return 0;
}
Expected output
2A mutex protects a shared operation from overlapping accesses. A lock_guard acquires the lock for its scope and releases it automatically, letting two attendance workers update one count safely.
Trace the result: Read the complete program below from its starting values to the output operations. Each std::cout statement displays the value supplied at that point; \n begins the next line. The resulting lines, in order, are 2. In this example, distinguish what the program calculates or changes from what it actually displays.
Important mistakes and boundaries
Every conflicting access must follow the same synchronization rule. A mutex does not promise which worker runs first and careless lock ordering can deadlock.
Change the school example
C++17 · edit this complete example
#include <iostream>
#include <thread>
#include <mutex>
int main() {
int count = 0;
std::mutex mutex;
auto add = [&] {
std::lock_guard<std::mutex> lock(mutex);
++count;
}
;
std::thread first(add);
std::thread second(add);
first.join();
second.join();
std::cout << count << '\n';
return 0;
}
Your task: Start the count at 5. Before running your edited program, predict the new output and explain which stored value, condition or object changes. Keep the rest of the example intact.
Use the complete source repeated here; this task does not depend on remembering code from another section.
Worked answer and reasoning
Worked variation: Start the count at 5. Both protected updates complete and the final count is 7.
A mutex protects a shared operation from overlapping accesses. A lock_guard acquires the lock for its scope and releases it automatically, letting two attendance workers update one count safely.
Every conflicting access must follow the same synchronization rule. A mutex does not promise which worker runs first and careless lock ordering can deadlock.
For a baseline run, save this source as main.cpp and use clang++ -std=c++17 -Wall -Wextra -pedantic main.cpp -o school, or equivalent g++ flags. Recompile after editing so that you run the updated executable.
The source and output below belong to the edited program. Compare them with the original example to see the consequence of this single change.
C++17 · worked variation · complete program
#include <iostream>
#include <thread>
#include <mutex>
int main() {
int count = 5;
std::mutex mutex;
auto add = [&] {
std::lock_guard<std::mutex> lock(mutex);
++count;
}
;
std::thread first(add);
std::thread second(add);
first.join();
second.join();
std::cout << count << '\n';
return 0;
}
Expected output
7Run in C++ compiler →Check your understanding
C++17 · original program for this question
#include <iostream>
#include <thread>
#include <mutex>
int main() {
int count = 0;
std::mutex mutex;
auto add = [&] {
std::lock_guard<std::mutex> lock(mutex);
++count;
}
;
std::thread first(add);
std::thread second(add);
first.join();
second.join();
std::cout << count << '\n';
return 0;
}
Check a common mistake
What to remember
A mutex protects a shared operation from overlapping accesses. Every conflicting access must follow the same synchronization rule. A mutex does not promise which worker runs first and careless lock ordering can deadlock.
Useful connections: Run a Task in Another Thread, Give a Task a Name.
Language and design guidance: C++ Core Guidelines and C++ working draft. The draft describes an evolving language; these examples deliberately target C++17.
Reaching the recap and completing the quizzes are separate milestones.