C语言/C++学习之C++多线程编程之多线程数据共享问题
小职 2021-03-09 来源 : 阅读 652 评论 0

摘要:本文主要介绍了C语言/C++学习之C++多线程编程之多线程数据共享问题,通过具体的内容向大家展现,希望对大家C语言/C++的学习有所帮助。

本文主要介绍了C语言/C++学习之C++多线程编程之多线程数据共享问题,通过具体的内容向大家展现,希望对大家C语言/C++的学习有所帮助。

C语言/C++学习之C++多线程编程之多线程数据共享问题

通过容器创建多个线程

 

#include <vector>

#include <iostream>

#include <thread>

void printTest(int num)  

{

    std::cout << "子线程:" << num << "启动" << std::endl;

    std::cout << "子线程:" << num << "结束" << std::endl;

}

int main()  

{

    std::vector<std::thread* > test;

    for (int i = 0; i < 10; i++)  

    {

        test.push_back(new std::thread(printTest, i));

    }

    for (auto& pmove : test)

    {

        pmove->join();

    }

    std::cout << "主线程" << std::endl;

    return 0;

}

数据共享问题分析只读数据:稳定安全,不需要特殊处理,直接读即可

 

#include <vector>

#include <iostream>

#include <thread>

std::vector<int> g_data={ 1,2,3 };

void printTest(int num)  

{

 std::cout << "子线程:" << num << "读操作" << std::endl;

 for (auto pmove : g_data)  

 {

 std::cout << pmove << std::endl;

 }

}

int main()  

{

 std::vector<std::thread* > test;

 for (int i = 0; i < 10; i++)  

 {

 test.push_back(new std::thread(printTest, i));

 }

 for (auto& pmove : test)

 {

 pmove->join();

 }

 std::cout << "主线程" << std::endl;

 return 0;

}

有读有写:需要做特别处理(写只做写,读只做读操作,保持共享数据只有唯一操作),不然会引发崩溃

 

#include <list>

#include <iostream>

#include <thread>

class SeaKing  

{

public:

 void makeFriend()

 {

 for (int i = 0; i < 100000; i++)  

 {

 std::cout << "增加一个" << std::endl;

 mm.push_back(i);

 }

 }

 void breakUp()  

 {

 for (int i = 0; i < 100000; i++)  

 {

 if (!mm.empty())  

 {

 std::cout << "减少一个:"<<mm.front() << std::endl;

 mm.pop_front();

 }

 else  

 {

 std::cout << "已空" << std::endl;

 }

 }

 }

protected:

 std::list<int> mm;

};

int main()  

{

 SeaKing man;

 std::thread t1(&SeaKing::makeFriend, &man);

 std::thread t2(&SeaKing::breakUp, &man);

 t1.join();

 t2.join();

 return 0;

}

//以上程序会异常退出

加锁的方式解决数据共享问题互斥量mutex: 互斥量可以理解为锁,他是一个mutex类的对象通过调用成员函数lock函数进行加锁通过调用成员函数unlock函数进行解锁

 

#include <list>

#include <iostream>

#include <thread>

#include <mutex> //1.包含头文件

class SeaKing  

{

public:

 void makeFriend()

 {

 for (int i = 0; i < 100000; i++)  

 {

 m_mutex.lock();

 std::cout << "增加一个" << std::endl;

 mm.push_back(i);

 m_mutex.unlock();

 }

 }

 bool readInfo()  

 {

 m_mutex.lock(); //2.加锁

 if (!mm.empty())

 {

 std::cout << "减少一个:" << mm.front() << std::endl;

 mm.pop_front();

 m_mutex.unlock();

 return true;

 }

 m_mutex.unlock();

 return false;

 }

 void breakUp()  

 {

 for (int i = 0; i < 100000; i++)

 {

 int result = readInfo();

 if (result == false)  

 {

 std::cout << "已空" << std::endl;

 }

 }

 }

protected:

 std::list<int> mm;

 std::mutex m_mutex; //创建互斥量对象

};

int main()  

{

 SeaKing man;

 std::thread t1(&SeaKing::makeFriend, &man);

 std::thread t2(&SeaKing::breakUp, &man);

 t1.join();

 t2.join();

 return 0;

}

注意:lock函数与unlock都是成对出现,如果lock了没有调用unlock会引发异常,abort终止程序通过lock_guard加锁。

 

#include <list>

#include <iostream>

#include <thread>

#include <mutex>

class SeaKing  

{

public:

    void makeFriend()

    {

        std::lock_guard<std::mutex> sbguard(m_mutex);

        for (int i = 0; i < 100000; i++)  

        {

            std::cout << "增加一个" << std::endl;

            mm.push_back(i);

        }

    }

    bool readInfo()  

    {

        std::lock_guard<std::mutex> sbguard(m_mutex);

        if (!mm.empty())

        {

            std::cout << "减少一个:" << mm.front() << std::endl;

            mm.pop_front();

            return true;

        }

        return false;

    }

    void breakUp()  

    {

        for (int i = 0; i < 100000; i++)

        {

            int result = readInfo();

            if (result == false)  

            {

                std::cout << "已空" << std::endl;

            }

        }

    }

protected:

    std::list<int> mm;

    std::mutex m_mutex;

};

int main()  

{

    SeaKing man;

    std::thread t1(&SeaKing::makeFriend, &man);

    std::thread t2(&SeaKing::breakUp, &man);

    t1.join();

    t2.join();

    return 0;

}

其实lock_guard 在构造函数中进行lock,在析构函数中进行unlock,本质上还是lock与unlock操作。


我是小职,记得找我

✅ 解锁高薪工作

✅ 免费获取学习教程,开发工具,代码大全,参考书籍

C语言/C++学习之C++多线程编程之多线程数据共享问题

本文由 @小职 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程