使用任何函数在线程中运行并传递服务器生成的值的服务器类 [英] A Server Class Taking Any Function To Run in Thread With Passing a Server-Generated Value
问题描述
我正在尝试使用一种方法编写服务器类,该方法将函数作为参数并将值(在服务器类中生成)传递给在线程内运行的函数.
I am trying to write a server class with a method that takes a function as an argument and pass in a value (generated inside the server class) to the function for being run inside a thread.
以下只是一个例子来说明我正在尝试做的事情.我的目标是让 Server::runFunctionInThread
足够通用,适用于不同类的方法以及不属于任何类的函数.这是可能的事情吗?
The following is just an example to illustrate what I am trying to do. My goal is to have the Server::runFunctionInThread
general enough that works for methods of different classes as well as for functions that do not belong to any classes. Is this a possible thing to do?
#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':找不到匹配的重载函数
.请问有人可以解释一下吗?
The compiler complains with an error 'std::invoke': no matching overloaded function found
. Could someone kindly shed light on this, please?
推荐答案
在我们遇到你的问题之前,你为什么不定义 Server
、A
和 >B
构造函数?
Before we got your problem, why you don't define the Server
, A
and B
constructors ?
所以,你的问题是函数参数.您刚刚向您的 Server
发送了一个函数地址:
So, your problem is with functions argument. You just sent a function address to your 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
参数.或者,如果您将代码更改为:
Because of non-member functions which don't have a self
parameter. Or if you change your code to this:
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
:
It won't work with member function, so i suggest you to use std::bind
and std::function
like this:
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));
这篇关于使用任何函数在线程中运行并传递服务器生成的值的服务器类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!