首页 > 编程语言 >[C++] std::thread 使用重载函数

[C++] std::thread 使用重载函数

时间:2023-01-10 12:14:08浏览次数:32  
标签:std struct thread readProcTask C++ tm include procs

出错代码

#include <thread>
#include <iostream>
#include <utility>
#include <vector>
#include <string>

char readProcTask(const std::string &cmd, struct timespec &sample_time, std::vector<ProcInfo> &procs) {
    ...
}

char readProcTask(int32_t pid, struct timespec &sample_time, std::vector<ProcInfo> &procs) {
    ...
}

int main() {
    // sample for 500 ms
    struct timespec tm{};
    tm.tv_sec = 0;
    tm.tv_nsec = 1 * 500 * 1000 * 1000; /* 500 ms */

    std::vector<ProcInfo> procs;
    printf("Roboeye_server:\n");
    readProcTask("Roboeye_server", tm, procs);
    std::thread aa(readProcTask, "Roboeye_server", tm, procs);
}

报错信息

No matching constructor for initialization of 'std::thread' candidate template ignored:
couldn't infer template argument '_Callable' candidate constructor not viable: 
requires 1 argument, but 4 were provided candidate constructor not viable: 
requires 1 argument, but 4 were provided candidate constructor not viable: 
requires 1 argument, but 4 were provided candidate constructor not viable: 
requires single argument '__t', but 4 arguments were provided candidate constructor not viable: 
requires 0 arguments, but 4 were provided

解决办法

重载函数的函数签名(signature)不同,是不同的函数。当开发者想绑定一个重载函数而仅给出名字时,编译器无法判定希望绑定的是哪一个函数,就会抛出编译错误。
所以当有作为参数传递的重载函数时,需要声明使用哪个重载函数。

正确代码

#include <thread>
#include <iostream>
#include <utility>
#include <vector>
#include <string>

char readProcTask(const std::string &cmd, struct timespec &sample_time, std::vector<ProcInfo> &procs) {
    ...
}

char readProcTask(int32_t pid, struct timespec &sample_time, std::vector<ProcInfo> &procs) {
    ...
}

int main() {
    // sample for 500 ms
    struct timespec tm{};
    tm.tv_sec = 0;
    tm.tv_nsec = 1 * 500 * 1000 * 1000; /* 500 ms */

    std::vector<ProcInfo> procs;
    printf("Roboeye_server:\n");
    readProcTask("Roboeye_server", tm, procs);
    using f1 = char (*)(const std::string &, struct timespec &, std::vector<ProcInfo> &);
    std::thread aa(static_cast<f1>(readProcTask), "Roboeye_server", std::ref(tm), std::ref(procs));
}

标签:std,struct,thread,readProcTask,C++,tm,include,procs
From: https://www.cnblogs.com/jiangyibo/p/17039752.html

相关文章

  • bzip2 C/C++ 库bzlib.h使用案例:使用实用函数进行压缩/解压缩
    bzip2提供了底层接口,高级接口以及两个实用函数(Utilityfunctions),这两个实用函数在无stdio的环境中也可以使用,它们分别是BZ2_bzBuffToBuffCompress和BZ2_bzBuffToBuffDecomp......
  • 面向对象程序设计 第二章 C++简单的程序设计
    目录C++语言的特点1.兼容C语言·它保持了C的简洁、高效和接近汇编语言等特点。·对C的类型系统进行了改革和扩充。·C++也支持面向过程的程序设计,不是一个纯正的面......
  • C++ read 读取字节数与设置不一样
    当需要读取二进制文件时,C++可以采用ofstream流,并设置模式为ios::binary,就可以通过read函数进行按照字节读取了。需要注意的是:如果模式未进行设置,默认将以文本方式读......
  • C++核心知识回顾(自定义数据类型)
    复习C++类自定义数据类型最灵活的方式就是使用C++的类结构现在定义一个货币类型Currency:enumsignType{PLUS,MINUS};classCurrency{public:Currency(signTy......
  • C++ move()函数及priority_queue队列使用记录
    最近刷leetcode题,使用了move()函数及优先队列(堆)priority_queue数据结构,记录一下!1.move函数move(obj)函数的功能是把obj当做右值处理,可以应用在对象的移动上。右值引用......
  • ThreadLocal底层原理
    文章目录1.什么是ThreadLocal?2.ThreadLocal基本用法3.ThreadLocal的应用场景4.ThreadLocal底层原理5.强软弱引用之间的区别5.1强引用5.2软引用5.3弱引用5.4虚引用6.Thr......
  • C++ATM取存款机模拟程序[2023-01-09]
    C++ATM取存款机模拟程序[2023-01-09]ATM取存款机模拟程序要求:设计一个程序,当输入给定的卡号和密码(初始卡号和密码为123456)时,系统能登录ATM取款机系统,用户可以按照以下......
  • ThreadLocal源码解析
    一、ThreadLocal概述ThreadLocal是一个线程的本地变量,也就意味着这个变量是线程独有的,是不能与其他线程共享的。这样就可以避免资源竞争带来的多线程的问题。但是,这种解......
  • C++引用【cherno课程学习】
    定义intmain(){inta=5;int*b=&a;//这个是指针int&ref=a;//这个是引用std::cin.get();}ref变量实际上不存在,只存在于源代码中,如果对......
  • C++实现顺序栈相关操作代码
    #include<iostream>#include<cstdlib>usingnamespacestd;#defineMAXSIZE100#defineOK1#defineERROR0#defineOVERFLOW-2typedefintStatus;typedefintElemtype......