c++ - 如何通过pthreads将类成员函数与类的对象绑定(bind)来创建线程?

标签 c++ pthreads

如何将类成员函数(没有输入参数)与类的对象绑定(bind),以便绑定(bind)的函数可以用于由 pthreads 创建线程?

#include <tr1/functional>
  using std::tr1::bind;
using namespace std::tr1::placeholders;


Server server(port_number);
Server* serverPtr;
serverPtr = &server; 

pthread_t my_thread = makeThread(bind(&lab1::Server::Run), serverPtr);

pthread_t makeThread(ThreadBody body) {

ThreadBody* copy = new ThreadBody(body);

void* arg = reinterpret_cast<void*>(copy);
pthread_t thread;
if (pthread_create(&thread, NULL, threadFunction, arg) != 0) {
perror("Can't create thread");
delete copy;
exit(1);
 }
return thread;
}

编译错误:

我不知道这个错误是什么意思。

/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/tr1/functional_iterate.h: In instantiation of âstd::tr1::_Result_of_implâ:
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/tr1/functional:126: instantiated from âstd::tr1::result_ofâ
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/tr1/bind_iterate.h:44: instantiated from âstd::tr1::_Bindâ
lab2.cpp:80: instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/tr1/functional_iterate.h:106: error: âintâ is not a class, struct, or union type
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/tr1/functional_iterate.h: In instantiation of âstd::tr1::_Result_of_implâ:
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/tr1/functional:126: instantiated from âstd::tr1::result_ofâ
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/tr1/bind_iterate.h:55: instantiated from âstd::tr1::_Bindâ
lab2.cpp:80: instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/tr1/functional_iterate.h:106: error: âconst intâ is not a class, struct, or union type
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/tr1/functional_iterate.h: In instantiation of âstd::tr1::_Result_of_implâ:
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/tr1/functional:126: instantiated from âstd::tr1::result_ofâ
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/tr1/bind_iterate.h:66: instantiated from âstd::tr1::_Bindâ
lab2.cpp:80: instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/tr1/functional_iterate.h:106: error: âvolatile intâ is not a class, struct, or union type
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/tr1/functional_iterate.h: In instantiation of âstd::tr1::_Result_of_implâ:
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/tr1/functional:126: instantiated from âstd::tr1::result_ofâ
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/tr1/bind_iterate.h:77: instantiated from âstd::tr1::_Bindâ
lab2.cpp:80: instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/tr1/functional_iterate.h:106: error: âconst volatile intâ is not a class, struct, or union type
lab2.cpp: In constructor âlab1::Server::Server(int)â:
lab2.cpp:80: error: could not convert âstd::tr1::bind [with _Functor = int, _T1 = sockaddr*, _T2 = long unsigned int](((lab1::Server*)this)->lab1::Server::listen_fd_, ((sockaddr*)(& server_endpoint)), 16ul)â to âboolâ
thread.hpp: In function âint main(int, char**)â:
thread.hpp:34: error: too many arguments to function âpthread_t base::makeThread(base::ThreadBody)â
lab2.cpp:229: error: at this point in file
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/tr1/bind_iterate.h: In member function âtypename std::tr1::result_of::type std::tr1::_Bind:perator()() [with _Functor = std::tr1::_Mem_fn]â:
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/tr1/functional_iterate.h:502: instantiated from âstatic void std::tr1::_Function_handler::_M_invoke(const std::tr1::_Any_data&) [with _Functor = std::tr1::_Bind ()()>]â
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/tr1/functional_iterate.h:850: instantiated from âstd::tr1::function::function(_Functor, typename std::__enable_if::_Useless, (! std::tr1::is_integral::value)>::__type) [with _Functor = std::tr1::_Bind ()()>, _Res = void]â
lab2.cpp:229: instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/tr1/bind_iterate.h:45: error: no match for call to â(std::tr1::_Mem_fn) ()â
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/tr1/functional_iterate.h:207: note: candidates are: _Res std::tr1::_Mem_fn:perator()(_Class&) const [with _Res = void, _Class = lab1::Server]
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/tr1/functional_iterate.h:213: note: _Res std::tr1::_Mem_fn:perator()(_Class*) const [with _Res = void, _Class = lab1::Server]
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/tr1/bind_iterate.h:45: error: return-statement with a value, in function returning 'void'
make: *** [lab2_server] Error 1

最佳答案

你应该像这样使用绑定(bind)

bind(&lab1::Server::Run, serverPtr);

但我认为 pthread_create 不接受 bind 返回的 function。 您可以在 Server 中编写静态辅助方法并使用 arg 参数。

class Server
{
  static void RunWrapper(void* server)
  {
     Server* s = static_cast<Server*>(server);
     s->Run();
  }
};


Server* myServer = CreateServer();
pthread_create(&thread, NULL, &Server::RunWrapper, myServer);

关于c++ - 如何通过pthreads将类成员函数与类的对象绑定(bind)来创建线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7304378/

相关文章:

c++ - pthread从类访问它,变量丢失,当直接工作正常时

c++ - 如何禁用 Dev C++ 中的提示?

c++ - 我怎样才能重新排列数组以更快地获得最小值、中值和最大值?

c - 不停止 gdb 中的所有线程

c++ - 线程处理 C++ linux OS

c++ - C++中的复制构造函数

c++ - 如何将 const 转换为非常量

c - 在c中使用互斥锁进行多线程并一次运行一个线程

linux - 如何确定 libpthread 和 libthread_db 是否匹配?

linux - pthread 和调用多个线程的正确方法