首页 > 其他分享 >std::thread 二:互斥量(多个互斥量的解决方法)

std::thread 二:互斥量(多个互斥量的解决方法)

时间:2023-06-18 23:11:33浏览次数:52  
标签:std mutex1 thread mutex2 lock 互斥 mutex

 

// *:这里的lock是函数模板,最少传两个互斥量

// 第一种,使用 lock  和  unlock
std::mutex m_mutex1;
std::mutex m_mutex2;

std::lock(m_mutex1, m_mutex2);
m_mutex1.unlock();
m_mutex2.unlock();


// 第二种,使用 lock 和 lock_guard
std::mutex m_mutex1;
std::mutex m_mutex2;

std::lock(m_mutex1, m_mutex2);
std::lock_guard<std::mutex> myguard1(m_mutex1, std::adopt_lock);
std::lock_guard<std::mutex> myguard2(m_mutex2, std::adopt_lock);

 

标签:std,mutex1,thread,mutex2,lock,互斥,mutex
From: https://www.cnblogs.com/shiyixirui/p/17489969.html

相关文章

  • std::thread 二:互斥量(带超时的互斥量 timed_mutex())
     timed_mutex、 try_lock_for、 try_lock_until #include<iostream>#include<thread>#include<mutex>#include<list>usingnamespacestd;classA{public:voidinNum(){for(inti=0;i<10000;i++)......
  • std::string 拼接字符串
      #include<iostream>#include<string>#include<sstream>usingnamespacestd;intmain(){stringa="123";stringstreamb;b<<123<<"456"<<789<<"-=-=";......
  • C++面试八股文:std::string是如何实现的?
    某日二师兄参加XXX科技公司的C++工程师开发岗位第18面:面试官:std::string用过吧?二师兄:当然用过(废话,C++程序员就没有没用过std::string的)。面试官:std::string("hello")+"world"、"hello"+std::string("world")和std::string("hello")+std::string("world")的......
  • C++面试八股文:std::string是如何实现的?
    某日二师兄参加XXX科技公司的C++工程师开发岗位第18面:面试官:std::string用过吧?二师兄:当然用过(废话,C++程序员就没有没用过std::string的)。面试官:std::string("hello")+"world"、"hello"+std::string("world")和std::string("hello")+std::string("world")的......
  • dremio 24.1 zstd 支持的的处理
    以前我简单介绍过关于dremio如何自己编译支持zstd压缩,目前官方24.1直接支持了,通过查看源码实际上处理思路以及方法与我介绍的是一致的,具体可以参考我写过的博客参考资料https://www.cnblogs.com/rongfengliang/p/16823130.html......
  • fastdfs配置文件说明
    参考网址一、tracker.conf#配置tracker.conf文件是否生效false生效true屏蔽disabled=false#程序的监听地址,如果不设定则监听所有地址(0.0.0.0)bind_addr=#tracker监听的端口port=22122#连接超时时间(秒)。#默认值为30。#注意:在内网(LAN)中,2秒就足够......
  • VirtualAllocEx;WriteProcessMemory;CreateRemoteThread
    /*structStrParam{ HWNDhPwdEdit; unsignedintnLenth; char*buff;};//计算器为目标进程。staticDWORDWINAPIMyFunc(LPVOIDpData){//dosomething StrParam*param=(StrParam*)pData; HWNDhPwdEdit=param->hPwdEdit; char*buff=param->buff; ......
  • boost::this_thread::sleep_for()死锁
    boost::this_thread::sleep_for()会死锁(金庆的专栏)发现睡眠1ms很容易死锁。boost::this_thread::sleep_for(boost::chrono::milliseconds(1)).Boost1.54.0以下代码很可能重现死锁:#include"stdafx.h"#include<iostream>#include<boost/thread.......
  • c++多线程 std::async std::future
    c++标准库中对线程操作有完善的封装,其中最常用到的如std::thread,std::async。EffectiveModernCpp中指出,应尽量使用std::async即基于任务的编程而非基于线程的编程。std::thread在前面的文章有提到过,此处仅对std::async作以记录。正如前面所说,std::async是基于任务的策略,本人理......
  • [问题解决]:ImportError: /home/test/anaconda3/envs/py39/bin/../lib/libstdc++.so.6
    报错(py39)test@test:~/code/Face/test_speed$pythonface_yaw_pitc_roll.pyTraceback(mostrecentcalllast):File"/home/test/code/Face/test_speed/face_yaw_pitc_roll.py",line17,in<module>importdlibFile"/home/test/anacon......