执行类的成员函数 [英] Executing a member function of a class
问题描述
SNIPPET 1:主题初始化(main_app.cpp)
#include< thread>
#includeServiceRegistrar.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是以下* /
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:编译输出
$ g ++ -g -c -Wall -std = c ++ 11 main_app.cpp -pthread
在/ usr / include / c ++ / 4.7 / ratio:38:0,
来自/usr/include/c++/4.7/chrono:38,
来自/usr/include/c++/4.7/thread:38,
来自main_app .cpp:8:
/usr/include/c++/4.7/type_traits:在实例化'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:需要从'struct std :: _ Bind_simple< std :: _ Mem_fn< void(ServiceRegistrar :: *)()> ServiceRegistrar)>'
/usr/include/c++/4.7/thread:133:9:需要从'std :: thread :: thread(_Callable&&&&& ...)[with _Callable = void(ServiceRegistrar :: *)(); _Args = {ServiceRegistrar&}]'
main_app.cpp:20:64:从这里需要
/usr/include/c++/4.7/type_traits:1834:9:错误: (std :: _ Mem_fn< void(ServiceRegistrar :: *)()>)(ServiceRegistrar)'
显然它是一个 gcc 4.7 bug ... use
std :: thread t(& ServiceRegistrar :: subscribe2TopologyServer, & sr1);
你可能不想将 sr1
复制到 t
的thread-local-storage,所以这更好
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)’
解决方案 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.
这篇关于执行类的成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!