技术专栏
Qt中实现C++线程操作二(线程同步)
Qt中实现C++线程操作二(线程同步)
线程同步是指同一进程中的多个线程互相协调工作从而达到一致性。之所以需要线程同步,是因为多个线程同时对一个数据对象进行修改操作时,可能会对数据造成破坏。
下面是多个线程同时修改同一数据造成破坏的例子:
#include <thread>
#include <iostream>
int count = 0;
void f1(){
for(int i = 0;i <1000;i++)
{
std::this_thread::sleep_for(std::chrono::milliseconds(1));
count ++;
std::cout << "f1--count:" << count << "\n";
}
}
void f2(){
for(int i = 0;i <1000;i++)
{
std::this_thread::sleep_for(std::chrono::milliseconds(1));
count ++;
std::cout << "f2--count:" << count << "\n";
}
}
int main(int argc, char *argv[])
{
std::thread t1(f1);
std::thread t2(f2);
t1.join();
t2.join();
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
<
运行结果:
由运行结果可以看出打印的数据并没有像想象的递增并且出现了相同的打印结果,这是因为两个线程同时访问了数据源并对数据源进行修改打印,所以出现了相同的打印结果。
想要解决这个问题就要进行线程同步。
C++的线程同步:mutex锁
#include <thread>
#include <mutex>
#include <iostream>
int count = 0;
std::mutex mutex;
void f1(){
for(int i = 0;i <1000;i++)
{
mutex.lock();
std::this_thread::sleep_for(std::chrono::milliseconds(1));
count ++;
std::cout << "f1--count:" << count << "\n";
mutex.unlock();
}
}
void f2(){
for(int i = 0;i <1000;i++)
{
mutex.lock();
std::this_thread::sleep_for(std::chrono::milliseconds(1));
count ++;
std::cout << "f2--count:" << count << "\n";
mutex.unlock();
}
}
int main(int argc, char *argv[])
{
std::thread t1(f1);
std::thread t2(f2);
t1.join();
t2.join();
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
<
运行结果:
加过锁的运行结果就没有再出现过数据打印错乱、重复的问题了,但是使用std::mutex每次都需要加锁和解锁,稍微不注意忘记解锁就会导致再也访问不了数据,使用std::lock_guard可以解决忘记解锁的问题
#include <thread>
#include <mutex>
#include <iostream>
int count = 0;
std::mutex mutex;
void f1(){
for(int i = 0;i <1000;i++)
{
std::lock_guard<std::mutex> lock(mutex);
std::this_thread::sleep_for(std::chrono::milliseconds(1));
count ++;
std::cout << "f1--count:" << count << "\n";
}
}
void f2(){
for(int i = 0;i <1000;i++)
{
std::lock_guard<std::mutex> lock(mutex);
std::this_thread::sleep_for(std::chrono::milliseconds(1));
count ++;
std::cout << "f2--count:" << count << "\n";
}
}
int main(int argc, char *argv[])
{
std::thread t1(f1);
std::thread t2(f2);
t1.join();
t2.join();
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
<
声明:本文内容由易百纳平台入驻作者撰写,文章观点仅代表作者本人,不代表易百纳立场。如有内容侵权或者其他问题,请联系本站进行删除。
红包
12
3
评论
打赏
- 分享
- 举报
评论
0个
手气红包

相关专栏
-
浏览量:6615次2020-10-28 23:03:59
-
浏览量:1833次2019-12-05 16:34:01
-
浏览量:1536次2019-12-05 16:50:26
-
浏览量:1825次2020-08-07 17:02:17
-
浏览量:1337次2022-10-10 11:44:39
-
浏览量:1337次2022-10-10 10:18:49
-
浏览量:4925次2021-09-16 13:47:50
-
浏览量:851次2023-09-20 19:02:57
-
浏览量:1748次2020-08-10 09:42:52
-
浏览量:835次2023-04-19 09:10:08
-
浏览量:2837次2020-07-29 14:50:28
-
浏览量:1054次2023-03-01 11:22:14
-
浏览量:3324次2020-08-30 11:44:43
-
浏览量:10640次2020-08-30 00:41:53
-
浏览量:2080次2020-08-22 16:10:10
-
浏览量:2417次2020-06-05 10:56:54
-
浏览量:8957次2020-11-12 21:51:54
-
浏览量:4622次2020-09-13 21:46:11
-
浏览量:9369次2020-12-06 23:24:07
置顶时间设置
结束时间
删除原因
-
广告/SPAM
-
恶意灌水
-
违规内容
-
文不对题
-
重复发帖
打赏作者

小王子🤴
您的支持将鼓励我继续创作!
打赏金额:
¥1

¥5

¥10

¥50

¥100

支付方式:

举报反馈
举报类型
- 内容涉黄/赌/毒
- 内容侵权/抄袭
- 政治相关
- 涉嫌广告
- 侮辱谩骂
- 其他
详细说明
审核成功
发布时间设置
发布时间:
请选择发布时间设置
是否关联周任务-专栏模块
审核失败
失败原因
请选择失败原因
备注
请输入备注