pthread_create 中的多个参数 [英] Multiple argument in pthread_create
本文介绍了pthread_create 中的多个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
根据pthread_create man page,该函数的参数是:
According to pthread_create man page, the argument of the function is:
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);
关于 void *arg
,我只是想知道我是否可以向它传递多个参数,因为我编写的函数需要 2 个参数.
regarding the void *arg
, I just wonder if I can pass multiple argument to it, because the function that I write requires 2 arguments.
推荐答案
使用 void*
你可以传递你选择的结构:
With your void*
you can pass a struct of your choosing:
struct my_args {
int arg1;
double arg2;
};
这有效地允许您传入任意参数.您的线程启动例程除了解压缩那些以调用真正的线程启动例程(它本身可能来自该结构)之外,什么也做不了.
This effectively allows you to pass arbitrary arguments in. Your thread start routine can do nothing other than un-pack those to call the real thread start routine (which could in itself come from that struct).
这篇关于pthread_create 中的多个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文