我正试图用一个方法编写一个服务器类,该方法将一个函数作为参数,并将一个值(在服务器类中生成(传递给该函数,以便在线程中运行。
以下只是一个例子来说明我正在尝试做什么。我的目标是使Server::runFunctionInThread
足够通用,适用于不同类的方法以及不属于任何类的函数。这有可能吗?
#include <iostream>
#include <thread>
#include <mutex>
#define __PRETTY_FUNCTION__ __FUNCSIG__
std::mutex my_mutex;
// *****************************************************************************************
class Server
{
public:
// With this class method, I am trying to get the function pointer A::FA, B::FB, and FC,
// then generate a value, and ultimately start a thread with the functions and
// the generated value.
template<class Function, class... Args>
void runFunctionInThread(Function f, Args&&... args) {
// This function does other tasks in addition to starting a thread and
// for the sake of this example, they are removed for simplicity.
int valueGeneratedInServer = rand() % 100;
// Pass in the generated value to the function and start a thread with it.
std::thread t(f, std::forward<Args>(args)..., valueGeneratedInServer);
t.detach();
}
};
// *****************************************************************************************
class A
{
public:
void FA(int value) {
std::unique_lock<std::mutex> lock(my_mutex);
std::cout << __PRETTY_FUNCTION__ << " value = " << value << "n";
}
void run() {
Server s;
s.runFunctionInThread(&A::FA, this);
}
};
// *****************************************************************************************
class B
{
public:
void FB(int value) {
std::unique_lock<std::mutex> lock(my_mutex);
std::cout << __PRETTY_FUNCTION__ << " value = " << value << "n";
}
void run() {
Server s;
s.runFunctionInThread(&B::FB, this);
}
};
// *****************************************************************************************
// A function that does not belong to any class
void FC(int value) {
std::unique_lock<std::mutex> lock(my_mutex);
std::cout << __PRETTY_FUNCTION__ << " value = " << value << "n";
}
// *****************************************************************************************
int main()
{
A a;
a.run();
B b;
b.run();
Server s;
s.runFunctionInThread(&FC, 3);
while (true) {};
}
编译器报告错误'std::invoke': no matching overloaded function found
。有人能解释一下吗?
在我们得到您的问题之前,为什么不定义Server
、A
和B
构造函数?
所以,你的问题是函数参数。您刚刚向Server
:发送了一个功能地址
s.runFunctionInThread(&A::FA, this);
...
s.runFunctionInThread(&B::FB, this);
...
s.runFunctionInThread(&FC, 3);
使用错误:
std::thread t(f, std::forward<Args>(args)..., valueGeneratedInServer);
因为非成员函数没有self
参数。或者,如果您将代码更改为:
template<class Function, class Arg>
void runFunctionInThread(Function f, Arg arg)
{
int valueGeneratedInServer = rand() % 100;
std::thread t(f, valueGeneratedInServer);
t.detach();
}
它不适用于成员函数,所以我建议您像这样使用std::bind
和std::function
:
template<class Function>
void runFunctionInThread(Function f)
{
int valueGeneratedInServer = rand() % 100;
std::thread t(f, valueGeneratedInServer);
t.detach();
}
并使用它:
对于成员功能:
Server s;
s.runFunctionInThread(std::bind(&A::FA, this, std::placeholders::_1));
对于非成员功能:
Server s;
s.runFunctionInThread(std::bind(&FC, 3));