首页 > 其他分享 >std::thread 二:互斥量(lock_guard())

std::thread 二:互斥量(lock_guard())

时间:2023-06-18 23:12:02浏览次数:48  
标签:std thread lock list 互斥 guard num mutex include

*:使用 lock_guard 后,就不可以使用 lock() 和 unlock() *:lock_guard 和智能指针一样,会自动解锁  

#include <iostream>
#include <thread>
#include <mutex>
#include <list>
using namespace std;

class A
{
public:
    void inNum()
    {
        for (int i = 0; i < 10000; i++)
        {
            m_mutex.lock();

            num_list.push_back(i);
            cout << "正在写:" << i << endl;

            m_mutex.unlock();
        }
    }

    void outNum()
    {
        for (int i = 0; i < 10000; i++)
        {
            if (!num_list.empty())
            {
                lock_guard<mutex> myguard(m_mutex);

                int temp_num = num_list.front();
                num_list.pop_front();
                cout << "正在读数据,当前数据:" << temp_num << endl;
            }
            else
            {
                cout << "正在读数据,当前数据:为空" << endl;
            }
        }
    }

    void parintListCount()
    {
        cout << "列表中剩余数量:" << num_list.size() << endl;
    }

private:
    list<int> num_list;
    mutex m_mutex;            // 创建一个互斥量
};



int main()
{
    A a;
    thread tIn(&A::inNum, &a);
    thread tOut(&A::outNum, &a);

    tIn.join();
    tOut.join();

    a.parintListCount();

    return 0;
}

 

           

标签:std,thread,lock,list,互斥,guard,num,mutex,include
From: https://www.cnblogs.com/shiyixirui/p/17489967.html

相关文章

  • std::thread 二:互斥量(多个互斥量的解决方法)
     //*:这里的lock是函数模板,最少传两个互斥量//第一种,使用lock和unlockstd::mutexm_mutex1;std::mutexm_mutex2;std::lock(m_mutex1,m_mutex2);m_mutex1.unlock();m_mutex2.unlock();//第二种,使用lock和lock_guardstd::mutexm_mutex1;std::mutexm_m......
  • 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秒就足够......
  • 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......
  • Linux C 编程——互斥锁mutex
    1、多线程的问题引入多线程的最大的特点是资源的共享,但是,当多个线程同时去操作(同时去改变)一个临界资源时,会破坏临界资源。如利用多线程同时写一个文件:#include<stdio.h>#include<pthread.h>#include<malloc.h>constcharfilename[]="hello";void*thread(void*id){......