当前位置: 首页 > news >正文

C++ 中的互斥锁

Mutex 代表互斥。在 C++ 中,std::mutex 类是一个同步原语,用于保护共享数据不被多个线程同时访问。共享数据可以是变量、数据结构等形式。

std::mutex类在 C++ 中实现互斥锁。它在头文件中定义。

C++ 中需要互斥锁
在 C++ 中,当多个线程同时修改相同的共享资源时,可能会引起竞争条件。它可能会在执行程序时产生不可预测的输出或意外行为。互斥锁用于通过锁定当前线程来避免竞争条件,以便所有其他线程当时无法访问共享资源,并在当前线程完成时解锁它。

C++ 中 Mutex 的语法
互斥锁的使用可以分为三步:

1.创建一个 std::mutex 对象

std::mutex mutex_object_name;
  1. 锁定线程
    std::mutex 类的lock ()函数锁定线程,并且只允许当前线程运行,直到解锁为止。它可以防止多个线程同时访问共享资源。
  2. 解锁线程
    std::mutex 函数的unlock ()用于在执行包含可能发生竞争条件的代码段后释放锁。它恢复所有等待的线程。
mutex_object_name.unlock ()

在这里插入图片描述
C++ 中的互斥锁示例
让我们创建一个共享整数变量,该变量可以在程序内部全局访问。创建一个函数,使用 for 循环将数字增加 1000000 次。创建两个名为 thread1 和 thread2 的线程来运行相同的increment() 函数。

在这种情况下,线程 1 将把数字加 1,执行 1000000 次,线程 2 将把数字加 1,执行 1000000 次。因此预期输出为 2000000。

然而,当多个线程试图同时修改同一资源时,可能会出现竞争条件。因此无法预测该数字的值

不使用互斥锁同步的代码

// C++ program to illustrate the race conditions 
#include <iostream>
#include <thread>

using namespace std;

// Shared resource
int number = 0;

// function to increment the number
void increment(){
    
    // increment number by 1 for 1000000 times
    for(int i=0; i<1000000; i++){
        number++;
    }
}

int main()
{
    // Create thread t1 to perform increment()
    thread t1(increment);
    
    // Create thread t2 to perform increment()
    thread t2(increment);
    
    // Start both threads simultaneously
    t1.join();
    t2.join();
    
    // Print the number after the execution of both threads
    cout << "Number after execution of t1 and t2 is " << number;
    
    return 0;
}

输出

执行同一程序三次,观察在没有线程同步的情况下修改共享资源时的行为。

Number after execution of t1 and t2 is 15923263lg@lg-vm:~/lg/c++/18$ ./a.out 
Number after execution of t1 and t2 is 13843590lg@lg-vm:~/lg/c++/18$ ./a.out 
Number after execution of t1 and t2 is 16377696lg@lg-vm:~/lg/c++/18$ ./a.out 
解释 很明显,程序的输出是不可预测的。当两个线程同时运行时,会导致竞争情况,从而产生不可预测的输出。不能保证输出为 2000000。这种不可预测的行为是由于使用多个线程同时修改同一个共享变量而发生的。

具有互斥同步的代码

// C++ program to illustrate the thread synchronization using mutex
#include <iostream>
#include <thread>

using namespace std;

// import mutex from C++ standard library
#include <mutex>

// Create object for mutex
mutex mtx;

// Shared resource
int number = 0;

// function to increment the number
void increment(){
    
    // Lock the thread using lock
    mtx.lock();
    
    // increment number by 1 for 1000000 times
    for(int i=0; i<1000000; i++){
        number++;
    }
    
    // Release the lock using unlock()
    mtx.unlock();
}

int main()
{
    // Create thread t1 to perform increment()
    thread t1(increment);
    
    // Create thread t2 to perform increment()
    thread t2(increment);
    
    // Start both threads simultaneously
    t1.join();
    t2.join();
    
    // Print the number after the execution of both threads
    std::cout<<"Number after execution of t1 and t2 is "<<number;
    
    return 0;
}

相关文章:

  • 2通道12bit 10G USB高速示波器采集卡
  • fastapi项目——后端返回前端url
  • layui.table.exportFile 导出数据并清除单元格中的空格
  • 【学习笔记】【SpringCloud】MybatisPlus 基础使用
  • Linux NFS
  • 【用deepseek和chatgpt做算法竞赛】——还得DeepSeek来 -Minimum Cost Trees_5
  • 自学Java-AI结合GUI开发一个石头迷阵的游戏
  • 人工智能丨OCR 的业务场景,实现原理和测试指标
  • HarmonyOS NEXT 全栈开发实战手册(API 12+)
  • 最新本地部署 DeepSeekR1 蒸馏\满血量化版 + WebOpenUI 完整教程(Ubuntu\Linux系统\Ollama)
  • 编译原理基础(1)
  • 4-知识图谱的抽取与构建-4_2实体识别与分类
  • Tesseract OCR使用
  • linux 麒麟安装人大金仓数据库
  • 革新之力:数字科技——重塑未来的超越想象之旅
  • AI基本知识讲解
  • java项目之超市管理系统设计与实现(源码+文档)
  • HTML项目一键打包工具:HTML2EXE 最新版
  • Lab13_ Visible error-based SQL injection
  • pika 支持的redis接口及兼容情况
  • 商务部就开展加强战略矿产出口全链条管控工作应询答记者问
  • 上海市重大工程一季度开局良好,崇明线等按既定计划加快建设
  • 视频丨美国两名男童持枪与警察对峙,一人还试图扣动扳机
  • 成都警方通报:8岁男孩落水父母下水施救,父亲遇难
  • 日本广岛大学一处拆迁工地发现疑似未爆弹
  • 从普通人经历中发现历史,王笛解读《线索与痕迹》