将回调函数中对对象的引用传递给 std::thread
Passing reference to object in callback function to std::thread
我正在尝试做这样的事情:
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()
中也遇到了最令人烦恼的解析错误。
- 在std::thread中,joinable()然后join()线程安全吗
- 分离一个静态常量 std::thread?
- 当指向对象的指针作为参数传递给 std::thread 时,内存可见性
- 如何从 std::thread 返回值
- 将 std::thread by 值推送到列表中
- 转发变量参数列表以模拟 std::thread
- 对 'std::thread::_M_start_thread CMake 的未定义引用进行基准测试
- std::thread 增加 DLL 引用计数,从而防止卸载 DLL
- 如何防止 std::thread 在 QT 中冻结 GUI?
- 对带有唯一指针的 std::thread 使用类成员函数时出现编译错误
- 为什么参数在构造 std::thread 时移动两次
- std::thread::_Invoker 使用线程编程时出错
- 在线程 A 中创建一个 std::thread 对象,在线程 B 中连接
- 为什么编译器抱怨 std::thread 参数在转换为右值后必须是可调用的?
- ZeroMQ 在使用 std::thread 创建工作线程时崩溃
- 在没有复制构造函数的对象的成员函数中启动 std::thread
- CLang:在 std::thread 中运行函数会导致结构创建BAD_ACCESS
- 调用以CWinThread为基的类运算符()的std::thread失败
- 如何通过std::thread生成多个线程
- std::future::get()或std::future::wait()是std::thread::join()的替