如果我要为以下函数创建一个 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;
}
最佳答案
这不起作用。 function() 必须只接受一个参数。这就是为什么你必须这样做:
(void * ()(void))
你告诉你的编译器“不,说真的,这个函数只接受一个参数”,当然事实并非如此。
您需要做的是传递一个参数(例如指向结构的指针),它可以为您提供所需的信息。
编辑:请参见此处的示例:number of arguments for a function in pthread
关于创建具有多个参数的函数的pthread,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19917211/