C++并发多线程--多个线程的数据共享和保护

目录

1--创建并等待多个线程

2--数据共享

2-1--数据只读

2-2--数据读写

2-3--共享数据保护简单案例


1--创建并等待多个线程

创建多个线程时,可以使用同一个线程入口函数;

多个线程的执行顺序与操作系统的调度机制有关,并不和创建线程的先后顺序相同;

一般会将多个线程对象存放在容器中,方便进行管理;

cpp 复制代码
#include <iostream>
#include <thread>
#include <vector>

// 线程入口函数
void myprint(int i){
    std::cout << "This is thread " << i << std::endl; 
}

int main(int argc, char *argv[]){
    std::vector<std::thread> V;
    // 创建5个线程
    for(int i = 0; i < 5; i++){
        V.push_back(std::thread(myprint, i));
    }

    // 等待5个线程
    for(auto iter = V.begin(); iter != V.end(); iter++){
        iter->join();
    }
    // 继续执行主线程
    std::cout << "This is main thread." << std::endl;
    return 0;
}

2--数据共享

2-1--数据只读

cpp 复制代码
#include <iostream>
#include <thread>
#include <vector>

// 共享只读数据
const std::vector<int> read_data = {1, 2, 3};

// 线程入口函数
void myprint(int i){
    // 只读数据
    std::cout << "thread " << i << " read data: " << read_data[0] << ","
    << read_data[1] << "," << read_data[2] << std::endl;
}

int main(int argc, char *argv[]){
    std::vector<std::thread> V;
    // 创建5个线程
    for(int i = 0; i < 5; i++){
        V.push_back(std::thread(myprint, i));
    }

    // 等待5个线程
    for(auto iter = V.begin(); iter != V.end(); iter++){
        iter->join();
    }
    // 继续执行主线程
    std::cout << "This is main thread." << std::endl;
    return 0;
}

2-2--数据读写

不同线程对共享数据进行读写时,可能会发生冲突,因此需要进行特殊的处理(例如加锁,线程间需互斥等);

2-3--共享数据保护简单案例

当两个线程对共享数据进行读写时,会发生冲突,可以通过互斥量(mutex) 来对共享数据进行加锁(lock) 、**解锁(unlock)**等操作;

lock() 与 unlock() 必须成对匹配,即调用次数相同;

必须使用同一个互斥量进行加锁和解锁;

需要选择正确的临界区进行上锁和解锁,即保护区域需正确;

cpp 复制代码
#include <iostream>
#include <thread>
#include <list>
#include <mutex> // 引入互斥

class myClass{
public:
    // 收集数据到消息队列
    void inMsgRecvQueue(){
        for(int i = 0; i < 100; i++){
            // 模拟收集消息
            std::cout << "Running inMsgRecvQueue(), insert one value: " 
                << i << std::endl; 

            // 选择正确的临界区进行加锁和解锁
            my_mutex.lock();
            msgRecvqueue.push_back(i); // 消息队列存储消息
            my_mutex.unlock();
        }
    }
    // 从消息队列取数据
    void outMsgRecvQueue(){
        for(int i = 0; i < 100; i++){
            if(!msgRecvqueue.empty()){
                // 选择正确的临界区并使用同一个互斥量进行加锁和解锁
                my_mutex.lock(); // 加锁
                // 取出数据
                int command = msgRecvqueue.front();
                msgRecvqueue.pop_front(); 

                my_mutex.unlock(); // 解锁
            }
            
            else{
                std::cout << "Running outMsgRecvQueue(), " 
                    "the msgRecvqueue is empty" << std::endl;
            }
        }
    }
private:
    std::list<int> msgRecvqueue; // 消息队列
    std::mutex my_mutex; // 创建互斥量 
};

int main(int argc, char *argv[]){
    myClass sample1;
    // 使用成员函数创建线程
    std::thread myInMsgObj(&myClass::inMsgRecvQueue, &sample1); // 收集数据线程
    std::thread myOutMsgObj(&myClass::outMsgRecvQueue, &sample1); // 取出数据线程
    myInMsgObj.join();
    myOutMsgObj.join();

    return 0;
}
相关推荐
W230357657314 小时前
经典算法:最长上升子序列(LIS)深度解析 C++ 实现
开发语言·c++·算法
.Ashy.14 小时前
2026.4.11 蓝桥杯软件类C/C++ G组山东省赛 小记
c语言·c++·蓝桥杯
minji...15 小时前
Linux 线程同步与互斥(三) 生产者消费者模型,基于阻塞队列的生产者消费者模型的代码实现
linux·运维·服务器·开发语言·网络·c++·算法
CoderCodingNo18 小时前
【GESP】C++三级真题 luogu-B4499, [GESP202603 三级] 二进制回文串
数据结构·c++·算法
hetao173383719 小时前
2026-04-09~12 hetao1733837 的刷题记录
c++·算法
6Hzlia19 小时前
【Hot 100 刷题计划】 LeetCode 136. 只出现一次的数字 | C++ 哈希表&异或基础解法
c++·算法·leetcode
汉克老师19 小时前
GESP2024年6月认证C++三级( 第二部分判断题(1-10))
c++·数组·位运算·补码·gesp三级·gesp3级
无限进步_20 小时前
【C++】只出现一次的数字 II:位运算的三种解法深度解析
数据结构·c++·ide·windows·git·算法·leetcode
小贾要学习21 小时前
【Linux】TCP网络通信编程
linux·服务器·网络·c++·网络协议·tcp/ip
哎嗨人生公众号21 小时前
手写求导公式,让轨迹优化性能飞升,150ms变成9ms
开发语言·c++·算法·机器人·自动驾驶