带参数的Pthread成员函数

Pthread member function with arguments

本文关键字:成员 函数 Pthread 参数      更新时间:2023-10-16

我正在尝试将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

如果你想在课堂上使用它,可以:

  1. 定义一个带有签名的静态方法:static void *myMethod(void *argument)
  2. 从对象(非静态)方法调用pthread_create,并传递this作为arg参数
  3. 静态方法的实现必须将void *argument强制转换为类的指针(类似的类型),该指针可用于调用对象的其他(非静态)方法

现在,根据您需要在线程中做的其他事情以及向它们传递参数,您可以做以下几件事:

  1. 与其在arg中传递this,不如传递一个可以包含this和所有参数的不同类型
  2. 或者,也许更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);