C++ linux pthread-有没有办法不将所有成员创建为静态的?
我使用pTrx创建线程(在C++应用程序中): 和CMyClass::RunThread必须是静态函数(才能编译): 因此,从RunThread调用的所有类成员和帮助函数也必须是静态的 看来我有很多(~5)个静态成员。(对我来说似乎不是一个好的编程…) 有更好的办法吗?更优雅的方式 谢谢C++ linux pthread-有没有办法不将所有成员创建为静态的?,c++,C++,我使用pTrx创建线程(在C++应用程序中): 和CMyClass::RunThread必须是静态函数(才能编译): 因此,从RunThread调用的所有类成员和帮助函数也必须是静态的 看来我有很多(~5)个静态成员。(对我来说似乎不是一个好的编程…) 有更好的办法吗?更优雅的方式 谢谢 将此指针传递给pthread_create 启动一个存根函数来解决这个问题 调用你的实际函数 伪代码如下所示: // static stub function void* MyClass::thread_stu
// static stub function
void* MyClass::thread_stub(void* p) {
MyClass* c = static_cast<MyClass*>(p);
return c->thread_func();
}
void* MyClass::thread_func() {
return NULL;
}
int MyClass::start() {
pthread_create(....thread_stub, this);
}
//静态存根函数
void*MyClass::线程_存根(void*p){
MyClass*c=静态铸件(p);
返回c->thread_func();
}
void*MyClass::thread_func(){
返回NULL;
}
int MyClass::start(){
pthread_create(..thread_stub,this);
}
我经常这样做,因此,我创建了一个用于执行成员函数的小模式
#include <pthread.h>
#include <iostream>
template<class T, void*(T::*thread_func)(void*)>
class pthread_launcher {
public:
pthread_launcher(T* obj=NULL, void* arg=NULL) : _obj(obj), _arg(arg) {}
void *launch() { return (_obj->*thread_func)(_arg);}
private:
/// Object pointer
T* _obj;
/// Command argument for member function
void *_arg;
};
// Launch thread function
template<class T>
void *launch_member_function(void *obj)
{
T* launcher = reinterpret_cast<T*>(obj);
return launcher->launch();
}
typedef struct thread_arg {
float fData;
} thread_arg_t;
class A {
public:
void* nonStaticFunction(void* arg) {
std::cout << "Executing" << std::endl;
return NULL;
}
};
// (1)
template class pthread_launcher<A,&A::nonStaticFunction>;
int main() {
thread_arg_t arg;
arg.fData = 1.0f;
// Laucher (2)
pthread_launcher<A, &A::nonStaticFunction> launcher;
// Initialize using function pointer and optional argument (2)
launcher = pthread_launcher<A,&A::nonStaticFunction>(&a, &arg);
A a;
pthread_t thread;
// Start thread (4)
pthread_create(&thread, NULL,
launch_member_function<pthread_launcher<A,&A::nonStaticFunction> >,
&launcher);
pthread_join(thread,NULL);
return 0;
}
#包括
#包括
模板
类pthread_启动器{
公众:
pthread_启动器(T*obj=NULL,void*arg=NULL):_obj(obj),_arg(arg){
void*launch(){return(_obj->*thread_func)(_arg);}
私人:
///对象指针
T*_obj;
///成员函数的命令参数
无效*_arg;
};
//启动线程函数
模板
void*启动\成员\功能(void*obj)
{
T*启动器=重新解释铸件(obj);
返回启动器->启动();
}
typedef结构线程参数{
浮动数据;
}螺纹参数;
甲级{
公众:
void*非静态函数(void*arg){
std::cout您可以使用std::thread
,它接受各种“可调用”类型,例如lamba、使用std::bind
的成员函数指针等。传递CMyClass
的对象作为pthread\u create
@JameyD:的最后一个参数,这只解决了“如何传递此
指针”的问题,而不是调用的函数必须可以直接调用pthread\u create
。严格来说,函数实际上必须具有C链接,因此不允许使用静态类成员函数。您的解决方案似乎很复杂,我更喜欢Calvin解决方案没问题。就个人而言,我非常喜欢通用解决方案:-)我自己的线程类实际上要复杂得多:)以支持动态调整线程数(即线程池)和优雅的终止机制。这里也一样。这只是一个片段,其中删除了一些跨平台的内容并处理信号。除一个线程外,所有线程都是阻塞信号,一个信号线程正在终止其他线程。
// static stub function
void* MyClass::thread_stub(void* p) {
MyClass* c = static_cast<MyClass*>(p);
return c->thread_func();
}
void* MyClass::thread_func() {
return NULL;
}
int MyClass::start() {
pthread_create(....thread_stub, this);
}
#include <pthread.h>
#include <iostream>
template<class T, void*(T::*thread_func)(void*)>
class pthread_launcher {
public:
pthread_launcher(T* obj=NULL, void* arg=NULL) : _obj(obj), _arg(arg) {}
void *launch() { return (_obj->*thread_func)(_arg);}
private:
/// Object pointer
T* _obj;
/// Command argument for member function
void *_arg;
};
// Launch thread function
template<class T>
void *launch_member_function(void *obj)
{
T* launcher = reinterpret_cast<T*>(obj);
return launcher->launch();
}
typedef struct thread_arg {
float fData;
} thread_arg_t;
class A {
public:
void* nonStaticFunction(void* arg) {
std::cout << "Executing" << std::endl;
return NULL;
}
};
// (1)
template class pthread_launcher<A,&A::nonStaticFunction>;
int main() {
thread_arg_t arg;
arg.fData = 1.0f;
// Laucher (2)
pthread_launcher<A, &A::nonStaticFunction> launcher;
// Initialize using function pointer and optional argument (2)
launcher = pthread_launcher<A,&A::nonStaticFunction>(&a, &arg);
A a;
pthread_t thread;
// Start thread (4)
pthread_create(&thread, NULL,
launch_member_function<pthread_launcher<A,&A::nonStaticFunction> >,
&launcher);
pthread_join(thread,NULL);
return 0;
}