0

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
Community
  • 1
  • 1
F. Aydemir
  • 2,665
  • 5
  • 40
  • 60
  • You could wrap the creation of the thread in a `try` block and add a `catch` block for a `system_error` exception. Then you could invoke `what()` on that exception and see if you get some useful information. – Andy Prowl Feb 28 '13 at 19:07
  • Wouldn't that just be "Operation not permitted" as shown? – Stephen Lin Feb 28 '13 at 19:20
  • @StephenLin: You're right. I don't know why I skipped that line – Andy Prowl Feb 28 '13 at 19:36

1 Answers1

1

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

Stephen Lin
  • 5,470
  • 26
  • 48
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • That I already did actually like: g++ -g -c -Wall -std=c++11 main_app.cpp ServiceRegistrar.hpp -pthread Any other suggestion? – F. Aydemir Feb 28 '13 at 19:06
  • @Fardaarda Sorry, apparently you need it in both steps just in case, because -pthread might set some defines (it depends on the compiler/platform) – Stephen Lin Feb 28 '13 at 19:54
  • [more info](http://stackoverflow.com/questions/2127797/gcc-significance-of-pthread-flag-when-compiling) – Stephen Lin Feb 28 '13 at 20:02
  • @Stephen Lin: I am a bit new to this compilation linkage thing and I wonder if what I wrote below is correct because I am still getiing this operation not permittable error.Thanks. g++ -g -c -Wall -std=c++11 main_app.cpp ServiceRegistrar.hpp -pthread -lpthread – F. Aydemir Mar 04 '13 at 09:38
  • @Fardaarda don't use `-lpthread`, just `-pthread` if you compiler supports the latter...and this is the compile step command (`-c`), when are you linking to create an executable? (i.e. `g++ -o main_app`...?) you should add `-pthread` there too. – Stephen Lin Mar 04 '13 at 13:55