Medium
One counter, three different callers
counter is thread_local. t1 calls increment_and_print(3), t2 calls increment_and_print(5) concurrently, then after both are joined, main itself calls increment_and_print(1). What values get printed for counter in the three calls (t1's line, t2's line, and main's line)? (The two threads' lines may print in either relative order, but focus on the values.)
main.cpp
#include <iostream>
#include <thread>
#include <mutex>
thread_local int counter = 0;
std::mutex io_mutex;
void increment_and_print(int times) {
for (int i = 0; i < times; ++i) {
++counter;
}
std::lock_guard<std::mutex> lock(io_mutex);
std::cout << "thread " << std::this_thread::get_id()
<< ": " << counter << std::endl;
}
int main() {
std::thread t1(increment_and_print, 3);
std::thread t2(increment_and_print, 5);
t1.join();
t2.join();
increment_and_print(1);
}73 of 117 in C++