首页 > 编程语言 >C++限制函数最大执行时间

C++限制函数最大执行时间

时间:2024-02-21 14:24:48浏览次数:41  
标签:std 限制 函数 thread lock C++ timeout cv

背景

C++调用某些硬件操作(如TPU推理)可能存在超时风险,需要限制函数的执行时间。

思考

异步执行免不了开线程,如何限制join的最大时间是关键。设计如下函数:

bool Infer(uint timeout_ms)

根据输入的timeout_ms参数,按时完成返回true超时返回false。

实现

使用std::mutex配合std::condiction_variable实现有限的join时间。

#include <mutex>
#include <condition_variable>

std::mutex m;
std::condiction_variable cv;

void DoWork()
{
    // do the stuff
    std::lock_guard<std::mutex> lock(m);
    cv.notify_all();
}

bool Infer(uint timeout_ms)
{
    std::thread(DoWork).detach();
    std::unique_lock<std::mutex> lock(m);
    std::cv_status status = cv.wait_for(lock, std::chrono::milliseconds(timeout_ms));
    if(std::cv_status::timeout == status)
    {
        return false;
    }
    return true;
}

DoWork()是类成员函数或需要入参,考虑使用lambda传入匿名functor

std::thread([&]{\
    DoWork(input, output);
    std::lock_guard<std::mutex> lock(m);
    cv.notify_all();
}).deatch();

拓展

对于已经超时的thread怎么处理?

In C++, once the thread is detached or uses the detach() function, then we cannot stop such threads, and still, if there is a need for stopping such threads, then only one way is to return the thread from the initial thread function by instantiating it in main() function, adding the Boolean value but before exiting ...

看来detached thread并无行之有效的结束方法,尝试强行调用std::terminate()会引发terminate called without an active exception
已知sub thread必须执行完成且无法安全退出,故可转换思路在main thread里做限制,发现超时强制休息N帧后再调用避免等待队列加深。

参考

std::condition_variable::wait_for - cppreference.com
Lambda expressions (since C++11) - cppreference.com
C++ thread detach | Working of thread detach() Function in C++

标签:std,限制,函数,thread,lock,C++,timeout,cv
From: https://www.cnblogs.com/azureology/p/18025004

相关文章

  • Bubbliiiing版本yolov7 c++opencv dnn部署
    使用B导的yolov7代码部署,代码地址:https://github.com/bubbliiiing/yolov7-pytorch 模型的的训练看B导即可,up主地址:Bubbliiiing的博客_CSDN博客-神经网络学习小记录,睿智的目标检测,有趣的数据结构算法领域博主 模型训练完成之后,在predict.py中设置mode="export_onnx"即可......
  • 玩转C语言:深入理解输入输出函数的奥秘
    ​✨✨欢迎大家来到贝蒂大讲堂✨✨......
  • 基于OpenVINO 2022.1 C++ API部署YOLOv7预训练模型
    任务背景作为视觉应用中最常见的任务之一,目标检测一直是各类新模型刷榜的必争之地,其中就以YOLO系列的网络结构最为突出。YOLO的全称是youonlylookonce,指只通过one-stage的方式需要“浏览一次”就可以识别出图中的物体的类别和位置。近期YOLO官方团队又放出新版本——YOLOv7,速......
  • c++ 直接读取 cpu id
    c++直接读取cpuid#include<iostream>usingnamespacestd;#include<string>#include<comutil.h>#include"Windows.h"#include<atlconv.h>#include<intrin.h>#include<cctype>#include<iomanip>char*......
  • C++多线程 第八章 设计并发代码
    第八章设计并发代码数据划分工作在处理开始前在线程间划分数据方面,C++与MPI或OpenMP的方式较为相似.一个任务被分成一个并行任务集,工作的线程独立运行这些任务.并且在最后的化简步骤中合并这些结果.尽管这种方法是很有效的,但是只有在数据可以实现划分时,才可如此.考虑这......
  • golang time包和日期函数
    获取当前时间 //获取当前时间对象 timeObj:=time.Now() /*获取当前日期语法一*/ //打印当前日期 fmt.Println(timeObj)//2024-02-2017:50:54.085353+0800CSTm=+0.000323093 //当前年 year:=timeObj.Year() //打印当月 month:=timeObj.Month() //......
  • MySQL——数据处理函数
    MySQL——数据处理函数数据处理函数又被称为单行处理函数,单行处理函数的特点:一个输入对应一个输出.语法格式:select单行处理函数(字段名)from表名;常见单行处理函数转换小写:lower()转换大写:upper()取子串:substr(),语法为substr(字段名,头,尾)。注意:起始下标是从1开......
  • 【C++】判断回文字符串。回文指的是顺读和逆读都一样的字符串。例如,“tot”和“otto”
    //判断字符串是否是回文字符串(考虑大小写,空格和标点符号)boolpalindrome1(string&str){stringret;for(auto&c:str){if(isalpha(c)){if(isupper(c)){ret.push_back(tolower(c));}else{ret.push_back(c);}......
  • python中的内置函数zip函数
    关于zip()函数,有几点要讲的。首先,官方文档中,它是这样描述的:Makeaniteratorthataggregateselementsfromeachoftheiterables.Returnsaniteratoroftuples,wherethei-thtuplecontainsthei-thelementfromeachoftheargumentsequencesoriterables.The......
  • input,type为number时隐藏点击按钮和限制输入最大最小值
    //隐藏点击按钮input::-webkit-outer-spin-button,input::-webkit-inner-spin-button{-webkit-appearance:none;}input[type='number']{-moz-appearance:textfield;}//解决输入中文后光标上移的问题.el-input__inner{line-height:1px!important;}//......