这是我之前在Executing a member function of a class 中发布的问题的后续。
我正在尝试以某种方式试验 C++1.1 线程,即它接受一个类的成员函数作为线程构造函数的参数,如下面第 20 行标记的第一个代码片段所示。类定义在第二个代码片段中给出。根据之前相关帖子中给出的答案,代码现在可以很好地编译。但是,现在我在第一个片段的第 20 行遇到了运行时错误。GDB 回溯显示在第三个片段中。我是 C++ 新手,无法正确解释此错误。你能告诉我有什么问题吗?谢谢。
片段 1:线程初始化(main_app.cpp)
#include <thread>
#include "ServiceRegistrar.hpp"
#define SERVER_TYPE 100065
#define SERVER_INST_LOWER 1
#define SERVER_INST_UPPER 2
#define TIMEOUT 500000
int main()
{
ServiceRegistrar sr1(SERVER_TYPE, TIMEOUT, SERVER_INST_LOWER, SERVER_INST_LOWER);
/*LINE 20 is the following*/
std::thread t(&ServiceRegistrar::subscribe2TopologyServer, &sr1);
t.join();
sr1.publishForSRs();
}
片段 2:类定义
class ServiceRegistrar
{
public:
ServiceRegistrar(int serverType, int serverTimeOut, int serverInstanceLower, int serverInstanceUpper)
: mServerType(serverType),
mServerTimeOut(serverTimeOut),
mServerInstanceLower(serverInstanceLower),
mServerInstanceUpper(serverInstanceUpper)
{ }
void subscribe2TopologyServer();
void publishForSRs();
void publishForServices();
private:
int mServerType;
int mServerTimeOut;
int mServerInstanceLower;
int mServerInstanceUpper;
};
片段 3:GDB 回溯
(gdb) r
Starting program: /home/......./src/main_app
terminate called after throwing an instance of 'std::system_error'
what(): Operation not permitted
Program received signal SIGABRT, Aborted.
0xb7fdd424 in __kernel_vsyscall ()
(gdb) bt
#0 0xb7fdd424 in __kernel_vsyscall ()
#1 0xb7d471df in raise () from /lib/i386-linux-gnu/libc.so.6
#2 0xb7d4a825 in abort () from /lib/i386-linux-gnu/libc.so.6
#3 0xb7f2e8ad in __gnu_cxx::__verbose_terminate_handler() ()
from /usr/lib/i386-linux-gnu/libstdc++.so.6
#4 0xb7f2c4f3 in ?? () from /usr/lib/i386-linux-gnu/libstdc++.so.6
#5 0xb7f2c52f in std::terminate() ()
from /usr/lib/i386-linux-gnu/libstdc++.so.6
#6 0xb7f2c7ce in __cxa_throw () from /usr/lib/i386-linux-gnu/libstdc++.so.6
#7 0xb7f8772e in std::__throw_system_error(int) ()
from /usr/lib/i386-linux-gnu/libstdc++.so.6
#8 0xb7f8883c in std::thread::_M_start_thread(std::shared_ptr<std::thread::_Impl_base>) () from /usr/lib/i386-linux-gnu/libstdc++.so.6
#9 0x0804981a in std::thread::thread<void (ServiceRegistrar::*)(), ServiceRegistrar*>(void (ServiceRegistrar::*&&)(), ServiceRegistrar*&&) (this=0xbffff050,
__f=
@0xbffff058: (void (ServiceRegistrar::*)(ServiceRegistrar * const)) 0x80491d2 <ServiceRegistrar::subscribe2TopologyServer()>)
at /usr/include/c++/4.7/thread:133
#10 0x08049526 in main () at main_app.cpp:20