带参数的Pthread成员函数
Pthread member function with arguments
我正在尝试将pthreads与类一起使用。我已经读到将线程与成员函数一起使用的最佳解决方案是定义一个静态辅助函数并从内部调用线程函数。但这需要将"this"指针作为参数传递给pthread_create。如果我的原始线程函数已经有一个参数,我该如何实现它?有没有办法将多个参数传递给pthread_create?
pthread_create定义为
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);
因此,它需要一个函数指针,例如
void *myfunction(void *argument)
和void* argument
如果你想在课堂上使用它,可以:
- 定义一个带有签名的静态方法:
static void *myMethod(void *argument)
- 从对象(非静态)方法调用pthread_create,并传递
this
作为arg
参数 - 静态方法的实现必须将
void *argument
强制转换为类的指针(类似的类型),该指针可用于调用对象的其他(非静态)方法
现在,根据您需要在线程中做的其他事情以及向它们传递参数,您可以做以下几件事:
- 与其在
arg
中传递this
,不如传递一个可以包含this
和所有参数的不同类型 - 或者,也许更OO,使线程功能所需的这些参数成为类的属性,这些属性可以在调用
pthread_create
之前在对象中设置,并且可以从类中获得
更具体的
class MyThread {
public:
MyThread(int _argument): mArgument(_argument) { }
void start() {
pthread_create(&mThreadId, 0,&MyThreadClass::threadMethod, this);
}
void doThings(int x) {
// something to be done in the thread.
}
static void *threadMethod(void *arg) {
MyThread *_this=static_cast<MyThread *>(arg);
_this->doThings(_this->getArgument());
}
int getArgument() const {
return mArgument;
}
private:
pthread_t mThreadId;
int mArgument;
};
这可以称为:
MyThread thread(10);
thread.start();
不能将多个参数传递给pthread_create
,但可以将多个自变量打包到专门为打包自变量而创建的struct
中。通过在cpp文件中而不是在头中定义struct
,使其成为实现的"私有"。将该结构的指针传递给pthread_create
,然后在助手中对其进行"解包"以调用成员函数。
假设线程实现是一个成员函数threadRun
,定义如下:
int MyClass::threadRun(int arg1, string arg2) {
... // Do useful work
return 42; // Return an important number
}
要调用此函数,请定义一个thread_args
struct
,如下所示:
struct thread_args {
MyClass *instance;
int arg1;
string arg2;
};
现在,您的助手函数可以定义如下:
void* thread_helper(void *voidArgs) {
thread_args *args = (thread_args*)voidArgs;
int res = args->instance->threadRun(args->arg1, args->arg2);
return new int(res); // Return an `int` pointer to pass back thread runner's results
}
启动线程的函数可能如下所示:
...
MyClass runner;
thread_args args;
args.instance = &runner;
args.arg1 = 123;
args.arg2 = "hello";
pthread_t thread_id;
int s = pthread_create(&thread_id, NULL, &thread_helper, &args);
相关文章:
- 对RValue对象调用的LValue ref限定成员函数
- 为什么使用 "this" 指针调用派生成员函数?
- 将公共但非静态的成员函数与ALGLIB集成
- 使用指向成员的指针将成员函数作为参数传递
- 将重载的成员函数传递给函数模板
- 我不小心调用了一个没有自己类对象的成员函数.但这是怎么回事呢
- 如何在C++中使用非静态成员函数作为回调函数
- C++错误C2600:无法定义编译器生成的特殊成员函数(必须首先在类中声明)
- 关联容器的下界复杂性:成员函数与非成员函数
- 在 C++ 中用派生类型重写成员函数
- 链表的泛型函数remove()与成员函数remove)
- 如何将lambda作为模板类的成员函数参数
- constexpr构造函数需要常量成员函数时出现问题
- 将自由函数绑定为类成员函数
- 区分非成员函数和头文件中的成员函数
- 如何从子成员函数修改父公共成员变量
- 保留对其他类的成员函数的引用
- 在运算符重载定义中使用成员函数(const错误)
- 内联如何影响模块接口中的成员函数
- 将成员函数指针作为参数传递给模板方法