将回调函数中对对象的引用传递给 std::thread

Passing reference to object in callback function to std::thread

本文关键字:std thread 引用 函数 回调 对象      更新时间:2023-10-16

我正在尝试做这样的事情:

void commands_conn_handler(int socket, RPiServer& server) {
// Not important code about handling connection
}
class RPiServer {
public:
void Accept(void Accept(void (*acceped_conn_handler)(int, RPiServer&)) {
// (...)
int remote_socket = 0; // Doesn't matter - example.
std::thread conn_handler_thread(acceped_conn_handler, remote_socket, *this);
conn_handler_thread.join();
}
};
int main() {
RPiServer commands_server();
commands_server.Accept(commands_conn_handler);
}

当我尝试构建它时,有一些错误:

在/usr/include/c++/6/thread:39:0 包含的文件中, 来自 src/rpi_server/rpiserver.cpp:11:/usr/include/c++/6/functional:在"struct std::_Bind_simple"的实例化中:/usr/include/c++/6/thread:138:26:从'std::thread::thread(_Callable&&, _Args&& ...)中需要[with _Callable = void (*&)(int, RPiServer&); _Args = {int&, RPiServer&}]' src/rpi_server/rpiserver.cpp:89:79:从这里需要/usr/include/c++/6/functional:1365:61:错误:在"类 std::result_of"中没有名为"type"的类型 typedef typename result_of<_Callable(_Args...)>::键入result_type; ^~~~~~~~~~~/usr/include/c++/6/functional:1386:9:错误:在"类 std::result_of"中没有名为"type"的类型 _M_invoke(_Index_tuple<_Indices...>) ^~~~~~~~~ 生成文件:29:目标"build/rpi_server/rpiserver.o"的配方失败

当我通过以下方式更改线程函数时(删除对对象的引用):

void commands_conn_handler(int socket) {
// Not important code about handling connection
}
class RPiServer {
public:
void Accept(void (*acceped_conn_handler)(int)) {
// (...)
int remote_socket = 0; // Doesn't matter - example.
std::thread conn_handler_thread(acceped_conn_handler, remote_socket);
conn_handler_thread.join();
}
};
int main() {
RPiServer commands_server();
commands_server.Accept(commands_conn_handler);
}

一切都很好。当我将引用作为参数传递给线程函数时,我做错了什么?

所以这里有一个工作示例:

#include <thread>
#include <functional>
class RPiServer;
void commands_conn_handler(int socket, RPiServer &server) {
// Not important code about handling connection
}
class RPiServer {
public:
void Accept(void (*acceped_conn_handler)(int, RPiServer&)) {
// (...)
int remote_socket = 0; // Doesn't matter - example.
std::thread conn_handler_thread(acceped_conn_handler, remote_socket, std::ref(*this));
conn_handler_thread.join();
}
};
int main() {
RPiServer commands_server;
commands_server.Accept(commands_conn_handler);
}

您遇到的错误是因为您没有为conn_handler_thread的构造函数提供正确的类型。若要显式获取对对象的引用(此处需要执行此操作),请使用std::ref()函数。

PS:您也复制粘贴了错误的代码示例,复制了void Accept部分。您在main()中也遇到了最令人烦恼的解析错误。