Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/141.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ std::thread是否使用参数包?如果是,则如何使用?:)_C++_Std_Variadic Functions_Stdthread_Parameter Pack - Fatal编程技术网

C++ std::thread是否使用参数包?如果是,则如何使用?:)

C++ std::thread是否使用参数包?如果是,则如何使用?:),c++,std,variadic-functions,stdthread,parameter-pack,C++,Std,Variadic Functions,Stdthread,Parameter Pack,我希望获得类似的结果: void somefunc(int var1, char var2){ dosomething(); } int main(){ std::thread t1(somefunc, 'a', 3); return EXIT_SUCCESS; } std::thread接受函数指针和任意数量的参数。在从std::thread派生的类中,我想用任意数量的参数初始化std::thread,但我得到一个错误,错误是: error: invalid conversion fr

我希望获得类似的结果:

void somefunc(int var1, char var2){
  dosomething();
}

int main(){
std::thread t1(somefunc, 'a', 3);
return EXIT_SUCCESS;
}
std::thread
接受函数指针和任意数量的参数。在从std::thread派生的类中,我想用任意数量的参数初始化std::thread,但我得到一个错误,错误是:

error: invalid conversion from ‘void (*)()’ to ‘void (*)(...)’ [-fpermissive]
   Thread<> t1(funptr, 30);
错误:从“void(*)()”到“void(*)(…)”[-fppermissive]的转换无效
螺纹t1(funptr,30);
但是当我删除标题中的点时,我会得到链接器错误。我还希望能够在没有参数的情况下运行它

我有以下代码(-stdc++17):

螺纹C.hpp

template </*typename Callable = void*,*/ typename... Args>
class Thread : public std::thread {
public:
    Thread(void (*function)(...), int ms, Args... args)
    :std::thread(/*std::forward<Callable>(function)*/function, ms, std::forward< Args >( args )...)
    {
        t_end = false;
    }
    ~Thread(){
        
    }
    void End(){
        t_end = true;
        if(joinable()){
            join();
        }
    }
    
private:
    void executor(/*Callable function*/void (*function)(...), int ms, Args... args){
        if(ms != 0){
            while(!t_end){
            (*function)(std::forward< Args >( args )...);
            std::this_thread::sleep_for(std::chrono::milliseconds(ms));
            }
        }else{
            (*function)(std::forward< Args >( args )...);
        }

        if(joinable()){
            join();
        }
        //std::async(std::launch::async, [] {
            //});
    }
    
    bool t_end;
};
模板
类线程:公共std::Thread{
公众:
线程(void(*函数)(…),int-ms,Args…Args)
:std::thread(/*std::forward(函数)*/function,ms,std::forward(Args)…)
{
t_end=false;
}
~Thread(){
}
void End(){
t_end=true;
if(可接合()){
join();
}
}
私人:
void执行器(/*可调用函数*/void(*函数)(…),int-ms,Args…Args){
如果(毫秒!=0){
而(!t_end){
(*函数)(标准::转发(Args)…);
std::this_线程::sleep_for(std::chrono::毫秒(ms));
}
}否则{
(*函数)(标准::转发(Args)…);
}
if(可接合()){
join();
}
//std::async(std::launch::async,[]{
//});
}
布尔图端;
};
main.cpp:

void func1(){
  dosomething();
}

void func2(int i){
  dosomething();
}

int main(){
  void (*funptr)() = &func1; // this step can be skipped still no result
  Thread<> t1(funptr, 30); // doesnt work
  Thread<int> t2(func2, 30, 3); // doesnt work
  Thread t2(func2, 30, 3); // doesnt work I want to be able to skip the template parameters like in the std::thread function

  return EXIT_SUCCESS;
}
void func1(){
dosomething();
}
无效函数2(int i){
dosomething();
}
int main(){
void(*funptr)(=&func1;//可以跳过此步骤,但仍然没有结果
线程t1(funptr,30);//不工作
线程t2(func2,30,3);//不工作
线程t2(func2,30,3);//不起作用我希望能够跳过std::Thread函数中的模板参数
返回退出成功;
}

如果有人能帮我,我会非常感激的。我仍然是C++初学者,在高中时:)代码>空(*函数)(…)<代码> >,>代码>空白(*)(…)<代码>不是一个“泛型函数指针”,它是指向(某个)变量函数的指针。您必须
模板线程(F函数)
或类似。或者只使用
std::function
。通常,只需在执行器中使用
std::function
,并通过
std::bind
生成一个。另外,您的“等待执行”并不好,因为它有很大的延迟-每次创建一个新线程更好。利用
std::condition\u variable
重用线程。使用模板时,我可以使模板参数可跳过吗?在std::thread中,您不必指定它们。或者我将尝试使用std::函数。