首页 > 其他分享 >std::thread 二:互斥量(带超时的互斥量 timed_mutex())

std::thread 二:互斥量(带超时的互斥量 timed_mutex())

时间:2023-06-18 23:11:06浏览次数:48  
标签:std thread 互斥 mutex timed include

 

timed_mutex 、 try_lock_for 、 try_lock_until

 

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


class A
{
public:
    void inNum()
    {
        for (int i = 0; i < 10000; i++)
        {
            std::cout << "写入一个数据:" << i << std::endl;

            // try_lock_for 和  try_lock_until 的作用是一样的
            std::chrono::microseconds timeOut(100);
            if (m_mutex.try_lock_for(timeOut))
            {
                // 100ms内拿到了锁
                m_num_list.push_back(i);
                m_mutex.unlock();
            }
            else
            {
                std::this_thread::sleep_for(timeOut);
            }

        }
    }

    void outNum()
    {
        int command = 0;
        while (true)
        {
            std::chrono::microseconds timeOut(100);
            if (m_mutex.try_lock_until(std::chrono::steady_clock::now() + timeOut))
            {
                if (m_num_list.empty())
                {
                    m_mutex.unlock();
                    continue;
                }

                command = m_num_list.front();
                m_num_list.pop_front();
                m_mutex.unlock();
                std::cout << "读取到数据了:" << command << std::endl;
            }
            else
            {
                std::this_thread::sleep_for(timeOut);
            }

            if (command == 9999) { break; }
        }
    }

private:
    std::timed_mutex m_mutex;
    std::list<int> m_num_list;
};

int main()
{
    // 创建对象
    A a;
    std::thread t1(&A::inNum, &a);
    std::thread t2(&A::outNum, &a);

    t1.join();
    t2.join();

    return 0;
}

 

标签:std,thread,互斥,mutex,timed,include
From: https://www.cnblogs.com/shiyixirui/p/17489972.html

相关文章

  • 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......
  • ScheduledThreadPoolExecutor模仿学习
     publicinterfaceCBlockingQueue<E>{booleanadd(Ee);Etake();} importjava.util.concurrent.Delayed;importjava.util.concurrent.FutureTask;importjava.util.concurrent.RunnableScheduledFuture;importjava.util.concurrent.TimeUnit;......