Search code examples
c++multithreadingc++11stdthread

Follow-up: Executing a member function of a class


This is a follow-up for the problem I posted earlier at Executing a member function of a class.

I am trying to experiment the C++1.1 threads in a way that it accepts a member function of a class as a parameter into the thread constructor as shown in the first code snippet below on line 20 that is marked . The class definition is given in the 2nd code snippet. The code compiles fine now based on the answer given in the earlier related post. However, now I get a runtime error at line 20 in the 1st snippet. The GDB backtrace is shown in the 3rd snippet. I am new to C++ and could not interpret this error properly. Could you please tell me what's wrong? Thanks.

SNIPPET 1: Thread initialization (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 definition

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 backtrace

  (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

Solution

  • This probably means that you haven't enabled threading support. Try adding -pthread to the compiler's command-line arguments (in the link step).