Multithreading C++;11将带参数的函数传递给线程中运行的函数

Multithreading C++;11将带参数的函数传递给线程中运行的函数,multithreading,c++11,Multithreading,C++11,我正在编写一个多线程TCP服务器。我将使用一个线程处理传入的套接字连接,并使用派生线程处理这些套接字上的通信。但是,我希望处理套接字连接的线程将带有支持参数的回调函数作为参数,而编译器正在抱怨。下面是说明我的问题的代码: template<class Function, class... Args> void handleIncomingConnectionRequests(Function&& f, Args... args) { f(args...); }

我正在编写一个多线程TCP服务器。我将使用一个线程处理传入的套接字连接,并使用派生线程处理这些套接字上的通信。但是,我希望处理套接字连接的线程将带有支持参数的回调函数作为参数,而编译器正在抱怨。下面是说明我的问题的代码:

template<class Function, class... Args>
void handleIncomingConnectionRequests(Function&& f, Args... args)
{
    f(args...);
}
    
void callback(int x)
{
    std::cout << x << "\n";
}
    
void main()
{
    std::thread handleIncomingConnectionsThread(handleIncomingConnectionRequests<decltype(callback)>, callback, 5);
    handleIncomingConnectionsThread.join();
}
为什么参数似乎没有参数?我显然是在向std::thread构造函数传递两个参数。我显然在传递变量的方式上做错了什么,但我不确定如何解决这个问题

编辑:有人指出我未能定义第二个模板参数。我已将代码更新为:

template<class Function, class... Args>
void handleIncomingConnectionRequests(Function&& f, Args... args)
{
    f(args...);
}
    
void callback(int x)
{
    std::cout << x << "\n";
}
    
void main()
{
    std::thread handleIncomingConnectionsThread(handleIncomingConnectionRequests<decltype(callback), int>, callback, 5);
    handleIncomingConnectionsThread.join();
}

此错误来自XTrk文件,是线程包含文件的C++标准库的一部分。

变更

std::thread handleIncomingConnectionsThread(handleIncomingConnectionRequests<decltype(callback), int>, callback, 5);
std::thread handleincomingconnectionthread(handleIncomingConnectionRequests,callback,5);

std::thread handleincomingconnectionthread(handleIncomingConnectionRequests,callback,5);

std::thread handleincomingconnectionthread(handleIncomingConnectionRequests,callback,5);
更改

std::thread handleIncomingConnectionsThread(handleIncomingConnectionRequests<decltype(callback), int>, callback, 5);
std::thread handleincomingconnectionthread(handleIncomingConnectionRequests,callback,5);

std::thread handleincomingconnectionthread(handleIncomingConnectionRequests,callback,5);

std::thread handleincomingconnectionthread(handleIncomingConnectionRequests,callback,5);

模板参数列表仅包含一种类型:
,还必须提供缺少的
int
。为什么不写:
std::thread th([](){handleIncomingConnectionRequests(callback,5);})
那么编译器可以推导出所有参数。@rafix07我已经修复了您指出的导致不同错误的错误。按照您的建议使用lambda确实可以解决问题,但似乎没有必要。一旦修复,前面的方法应该可以工作,所以从严格的学习角度来看,我很想知道为什么。我还想看看lambda会带来什么(如果有的话)额外的开销——在这种情况下,我没有性能限制,但我总是记住这一点。不过,感谢您为我的问题提供了解决方案!模板参数列表仅包含一种类型:
,还必须提供缺少的
int
。为什么不写:
std::thread th([](){handleIncomingConnectionRequests(callback,5);})
那么编译器可以推导出所有参数。@rafix07我已经修复了您指出的导致不同错误的错误。按照您的建议使用lambda确实可以解决问题,但似乎没有必要。一旦修复,前面的方法应该可以工作,所以从严格的学习角度来看,我很想知道为什么。我还想看看lambda会带来什么(如果有的话)额外的开销——在这种情况下,我没有性能限制,但我总是记住这一点。不过,感谢您为我的问题提供了解决方案!
std::thread handleIncomingConnectionsThread(handleIncomingConnectionRequests<decltype(callback), int>, callback, 5);
std::thread handleIncomingConnectionsThread(handleIncomingConnectionRequests<decltype(callback)*, int>, callback, 5);
std::thread handleIncomingConnectionsThread(handleIncomingConnectionRequests<decltype(&callback), int>, callback, 5);