服务器 Class 在线程中使用任何函数 运行 并传递服务器生成的值
A Server Class Taking Any Function To Run in Thread With Passing a Server-Generated Value
我正在尝试使用一种方法编写服务器 class,该方法将函数作为参数并将值(在服务器 class 内部生成)传递给函数 运行 在线程中。
以下只是一个例子来说明我正在尝试做的事情。我的目标是让 Server::runFunctionInThread
足够通用以适用于不同 classes 的方法以及不属于任何 classes 的函数。这可行吗?
#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));
我正在尝试使用一种方法编写服务器 class,该方法将函数作为参数并将值(在服务器 class 内部生成)传递给函数 运行 在线程中。
以下只是一个例子来说明我正在尝试做的事情。我的目标是让 Server::runFunctionInThread
足够通用以适用于不同 classes 的方法以及不属于任何 classes 的函数。这可行吗?
#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));