#include <iostream>
#include <thread>
#include <mutex>
using namespace std;
mutex mutexObject; // 互斥量对象
int refCount = 0;
int number = 0;
void crazyAdd(){
for (int i = 0; i < 1e7; ++i){number++;}
mutexObject.lock();
refCount--;
mutexObject.unlock();
}
int main(){
const int ThreadsCount = 5;
thread t[ThreadsCount];
refCount = ThreadsCount;
// 创建线程
for (int i = 0; i < ThreadsCount; i++){
t[i] = thread(crazyAdd);
t[i].detach();
}
// 等待线程完成
while (true){
lock_guard<mutex> lock(mutexObject);
if (refCount == 0){break;}
}
cout << number << endl;
return 0;
}