创建多参数函数的pthread

Ren*_*Liu 4 pthreads

如果我要为以下函数创建一个 pthread。

假设一切正常。

pthread_create(&threadId, &attr, (void * (*)(void*))function, //what should be the arguments for here??);
int a = 0;
int b = 1;
//c and d are global variables.

void function(int a, int b){
    c = a;
    d = b;
}
Run Code Online (Sandbox Code Playgroud)

Pet*_*ree 5

这不起作用。function() 必须只接受一个参数。这就是为什么你必须这样做:

(void * ( )(void ))

你告诉你的编译器“不,说真的,这个函数只接受一个参数”,这当然不是。

相反,您必须做的是传递一个参数(比如指向结构的指针),它为您提供所需的信息。

编辑:参见此处的示例:pthread 中函数的参数数量