首页 > 编程语言 >c++共享锁shared_mutex

c++共享锁shared_mutex

时间:2023-08-01 22:25:13浏览次数:31  
标签:target int lock c++ mutex fun shared

shared_mutex

  • shared_mutex::lock()用法同mutex::lock()
  • shared_mutex::lock_shared()允许多线程同时进入临界区,只用用于只读场景,不然是线程不安全的
  • shared_mutex::lock_shared()与shared_mutex::lock()互斥,不能同时上锁
#include <shared_mutex>
#include <iostream>
#include <thread>
using namespace std;

shared_mutex m;
int share_counter{0};

void increment1(int target)
{
    for (int i = 0; i < target; i++)
    {
        m.lock();
        share_counter++;
        m.unlock();
    }
}

void increment2(int target)
{
    for (int i = 0; i < target; i++)
    {
        m.lock_shared();
        share_counter++;
        m.unlock_shared();
    }
}

using incre_fun = void(int);
void test_increment(int target, incre_fun fun)
{
    share_counter = 0;
    thread t1(fun, target);
    thread t2(fun, target);
    t1.join();
    t2.join();
    cout << "Count result: " << share_counter << endl;
}

void test_increment2(int target, incre_fun fun1, incre_fun fun2)
{
    share_counter = 0;
    thread t1(fun1, target);
    thread t2(fun2, target);
    t1.join();
    t2.join();
    cout << "Count result: " << share_counter << endl;
}

void SharedMutexTest()
{
    test_increment(1000000, increment1);              // Count result: 2000000
    test_increment(1000000, increment2);              // Count result: 1906613
    test_increment2(1000000, increment1, increment2); // Count result: 2000000
}

标签:target,int,lock,c++,mutex,fun,shared
From: https://www.cnblogs.com/BuzzWeek/p/17599259.html

相关文章

  • C++入门到放弃(06)——this指针
    1.基本介绍this本身很容易理解:在C++所有类当中,都将this(关键字)指针设置为当前对象的地址。this本身是指针,*this是变量,类型为当前类的类型。2.举例刚开始看到this指针的时候,总会觉得奇怪,怎么会有这种用法。我们需要当前类的变量以及函数的时候,明明可以直接在类的内部直接调用,......
  • C++内存管理基础
    在c语言中内存管理函数为malloc和free,而在c++中内存管理的函数则是new和delete。首先来看new和delete对于申请的内置类型的空间是如何处理的内置类型的处理申请连续的多个空间voidtest1(){ int*ret=(int*)malloc(sizeof(int)*10); int*rett=newint[10];//和malloc一......
  • C++入门到放弃(05)——内联函数: inline
    ​1.基本用法要使用内联函数时,必须遵循如下规则:在函数声明前加上关键字inline,或着在函数定义前加上关键字inline,二者取其一即可。inlineintmax(inta,intb);//取其一即可inlineintmax(inta,intb){//取其一即可returna>b?a:b;}另外一种做......
  • ubuntu系统conda下运行pytorch报错:ImportError: libopenblas.so.0: cannot open share
    如题:ubuntu系统conda下运行pytorch报错:ImportError:libopenblas.so.0:cannotopensharedobjectfile   网上找了一些资料,基本都是自己下载openblas源码进行编译,不过突然之间相当conda环境提供一定的编译好的lib环境,使用conda命令既可安装,于是按照这个思路再进行搜索......
  • C++ Primer 学习笔记——第九章
    第9章顺序容器前言本章是对第三章——字符串、向量和数组的扩展延伸,在第三章我们对标准库的顺序容器有一定了解,那么学习完本章我们对顺序容器的知识将会更加完整。标准库定义了几种关联容器,关联容器中元素的位置由元素相关联的关键字值决定。我们将在本章对关联容器做一定了解......
  • 【C++】PACS基础知识学习
    1、PACSPACS是PictureArchivingandCommunicationSystems首字母缩写,全称为影像储存和传输系统,涉及放射医学、计算机技术、通讯技术及数字图像技术等,是医院信息系统的重要组成部分,是将数字医疗设备(如X线、CT、MRI、超声、病理等)所产生的医疗图像进行获取、储存、管理、诊断及......
  • C++函数传递函数指针、仿函数、绑定器、可调用对象
    只定义voidtestFunc(intnum,conststd::function<int(int)>&functor)就可以,其他的相当于这个函数的特化版本#include<iostream>#include<functional>usingnamespacestd;intfunc1(intnum){cout<<"func1:"<<num<<en......
  • 《dll篇》c#调用c++dll
    c#调用c++dll参考链接:https://jingyan.baidu.com/article/5d6edee2da72d699eadeecfe.html生成dll环境:VS2008新建项目->VisualC++->Win32项目MyDLL注意:C++编写的dll一般是不能直接拿来C#调用,需要先新建个C++的工程把dll里的方法重新封装成可被C#外部调用的函数。MyDLL.cp......
  • c++多线程同步
    死锁问题1单核实时可抢占的系统中,优先级不同的三个线程A/B/C,A>B>C当C先获得时间片开始执行,并获得锁A因为高优先级,被唤醒并中断C,但没有得到锁,而阻塞B获得执行机会,由于优先级高于C,B会一直执行,让AC系统无法取得任何进展std::stack<T>stack;std::mutexmutex;voidpush(c......
  • RichEdit RTF格式文本的存储和读取 VC++
    Rtf格式文本的读写,向控件发送EM_STREAMIN和EM_STREAMOUT消息,控件把格式文本显示或从把显示内容转化为格式文本。接口是用户定义的回调函数,格式文本由参数DWORD_PTRdwCookie传递。从控件读出RTF文本,参数用string比较好,因为消息处理是根据文本的长度,多次调用回调函数,传出数......