本文介绍了创建多参数函数的pthread的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我要为以下函数创建一个 pthread.
If I am going to create a pthread for the following function.
假设一切正常.
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() 必须只接受一个参数.这就是为什么你必须这样做:
This does not work. function() has to take exactly one argument. That's why you have to do this:
(void * ()(void))
(void * ()(void))
你告诉你的编译器不,说真的,这个函数只接受一个参数",当然它没有.
You're telling your compiler "no, seriously, this function only takes one argument", which of course it doesn't.
你必须做的是传递一个参数(比如一个指向结构的指针),它为你提供所需的信息.
What you have to do instead is pass a single argument (say a pointer to a struct) which gets you the information you need.
参见此处的示例:numberpthread 中函数的参数
这篇关于创建多参数函数的pthread的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!