#include <iostream>
#include <thread>
#include <mutex>
using namespace std;
void func(mutex& mtx, int* count)
{
int local = 0;
for (int i = 0; i < 10000000; i++)
{
mtx.lock();
++(*count);
mtx.unlock();
}
local += 1;
std::cout << local << std::endl;
};
int main()
{
int count = 0;
mutex mtx;
thread t1(func, std::ref(mtx), &count);
thread t2(func, std::ref(mtx), &count);
t1.join();
t2.join();
cout << count << endl;
return 0;
}
2 thread で count を加算。