c++实现多线程同步

 线程同步是指同一进程中的多个线程互相协调工作从而达到一致性。之所以需要线程同步,是因为多个线程同时对一个数据对象进行修改操作时,可能会对数据造成破坏,下面是多个线程同时修改同一数据造成破坏的例子:

复制代码
 1 #include <thread>
 2 #include <iostream>
 3 
 4 void Fun_1(unsigned int &counter);
 5 void Fun_2(unsigned int &counter);
 6 
 7 int main()
 8 {
 9     unsigned int counter = 0;
10     std::thread thrd_1(Fun_1, counter);
11     std::thread thrd_2(Fun_2, counter);
12     thrd_1.join();
13     thrd_2.join();
14     system("pause");
15     return 0;
16 }
17 
18 void Fun_1(unsigned int &counter)
19 {
20     while (true
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值