这是我之前在Executing a member function of a class发布的问题的后续跟踪。
我试图以一种方式试验C ++ 1.1线程,它接受类的成员函数作为线程构造函数的参数,如下面第20行标记的第一个代码片段所示。类定义在第二个代码片段中给出。根据之前相关帖子中给出的答案,代码现在编译得很好。但是,现在我在第1段中的第20行遇到了运行时错误。 GDB回溯显示在第3个片段中。我是C ++的新手,无法正确解释此错误。你能告诉我什么是错的吗?感谢。
SNIPPET 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();
}
SNIPPET 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;
};
SNIPPET 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
答案 0 :(得分:1)
这可能意味着您尚未启用线程支持。尝试将-pthread
添加到编译器的命令行参数(在链接步骤中)。