3

I am trying to experiment the C++11 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. When this code is compiled, I am getting a bunch of errors shown in the 3rd snippet. Can anyone tell me what I am doing 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: Compilation output

 $ g++ -g -c -Wall -std=c++11 main_app.cpp -pthread In file included from /usr/include/c++/4.7/ratio:38:0, from /usr/include/c++/4.7/chrono:38, from /usr/include/c++/4.7/thread:38, from main_app.cpp:8: /usr/include/c++/4.7/type_traits: In instantiation of ‘struct std::_Result_of_impl<false, false, std::_Mem_fn<void (ServiceRegistrar::*)()>, ServiceRegistrar>’: /usr/include/c++/4.7/type_traits:1857:12: required from ‘class std::result_of<std::_Mem_fn<void (ServiceRegistrar::*)()>(ServiceRegistrar)>’ /usr/include/c++/4.7/functional:1563:61: required from ‘struct std::_Bind_simple<std::_Mem_fn<void (ServiceRegistrar::*)()>(ServiceRegistrar)>’ /usr/include/c++/4.7/thread:133:9: required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (ServiceRegistrar::*)(); _Args = {ServiceRegistrar&}]’ main_app.cpp:20:64: required from here /usr/include/c++/4.7/type_traits:1834:9: error: no match for call to ‘ (std::_Mem_fn<void (ServiceRegistrar::*)()>) (ServiceRegistrar)’ 
1
  • 1
    ewwww very unnecessary macros. Commented Feb 28, 2013 at 19:00

2 Answers 2

4

Apparently it's a gcc 4.7 bug...use

std::thread t(&ServiceRegistrar::subscribe2TopologyServer, &sr1); 

instead.

EDIT: actually, you probably don't want to be copying sr1 to the thread-local-storage of t, so this is better anyway.

Sign up to request clarification or add additional context in comments.

2 Comments

Bug or not, you almost certainly want to bind to a pointer or reference here anyway.
@MikeSeymour yep, just added that
0

Try:

std::thread t(std::bind(&ServiceRegistrar::subscribe2TopologyServer, sr1)); 

Hope it helps.

1 Comment

That masks the problem, since bind doesn't share the same bug as thread. But binding to a copy of sr1 is almost certainly the wrong thing to do.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.