#include<iostream>
#include<thread>
#include<mutex>
using namespace std;
mutex mtx;//互斥量
void test(int &a) {
for (int i = 0; i < 10000; i++) {
mtx.lock();//获取互斥量的所有权
a++;
mtx.unlock();//释放互斥量的所有权
}
}
int main() {
int a = 0;
thread t1(test,ref(a));
thread t2(test,ref(a));
t1.join();
t2.join();
cout << a;
return 0;
}
多线程之互斥量解决多线程数据共享问题
最新推荐文章于 2024-07-13 19:27:27 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)