Qt中实现C++线程操作二(线程同步)

小王子🤴 2020-10-29 10:30:00 3792
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;
}

运行结果:

由运行结果可以看出打印的数据并没有像想象的递增并且出现了相同的打印结果,这是因为两个线程同时访问了数据源并对数据源进行修改打印,所以出现了相同的打印结果。
想要解决这个问题就要进行线程同步。
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;
}

运行结果:

加过锁的运行结果就没有再出现过数据打印错乱、重复的问题了,但是使用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;
}
声明:本文内容由易百纳平台入驻作者撰写,文章观点仅代表作者本人,不代表易百纳立场。如有内容侵权或者其他问题,请联系本站进行删除。
红包 12 3 评论 打赏
评论
0个
内容存在敏感词
手气红包
    易百纳技术社区暂无数据
相关专栏
置顶时间设置
结束时间
删除原因
  • 广告/SPAM
  • 恶意灌水
  • 违规内容
  • 文不对题
  • 重复发帖
打赏作者
易百纳技术社区
小王子🤴
您的支持将鼓励我继续创作!
打赏金额:
¥1易百纳技术社区
¥5易百纳技术社区
¥10易百纳技术社区
¥50易百纳技术社区
¥100易百纳技术社区
支付方式:
微信支付
支付宝支付
易百纳技术社区微信支付
易百纳技术社区
打赏成功!

感谢您的打赏,如若您也想被打赏,可前往 发表专栏 哦~

举报反馈

举报类型

  • 内容涉黄/赌/毒
  • 内容侵权/抄袭
  • 政治相关
  • 涉嫌广告
  • 侮辱谩骂
  • 其他

详细说明

审核成功

发布时间设置
发布时间:
是否关联周任务-专栏模块

审核失败

失败原因
备注
拼手气红包 红包规则
祝福语
恭喜发财,大吉大利!
红包金额
红包最小金额不能低于5元
红包数量
红包数量范围10~50个
余额支付
当前余额:
可前往问答、专栏板块获取收益 去获取
取 消 确 定

小包子的红包

恭喜发财,大吉大利

已领取20/40,共1.6元 红包规则

    易百纳技术社区