创建具有多个参数的函数的pthread

标签 c 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;
}

最佳答案

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

(void * ()(void))

你告诉你的编译器“不,说真的,这个函数只接受一个参数”,当然事实并非如此。

您需要做的是传递一个参数(例如指向结构的指针),它可以为您提供所需的信息。

编辑:请参见此处的示例:number of arguments for a function in pthread

关于创建具有多个参数的函数的pthread,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19917211/

相关文章:

c - 我的 arm-none-eabi-gcc 在 stm32 上的项目有什么问题?

c - 关于 pthread_kill() 行为

c - 使用内存泄漏的 pthread

c - 为什么关闭管道需要很长时间才能终止子进程?

c - pthread比 “default”版本慢

c - 反转数字数组

c - gmtime 函数 free() : invalid pointer

c - 重新分配指针数组

C/OSX/clang 困惑 : "symbol(s) not found" happening at link time instead of compile time

c - 从当前线程中找出事件线程的状态。 (稳健互斥体的实现)