因为接口仅仅定义了一个入參void *arg
int pthread_create(pthread_t *tidp,const pthread_attr_t *attr, (void*)(*start_rtn)(void*),void *arg);
所以,假设想传參数,须要封装结构体。将多个參数通过一个结构体传入线程。
typedef struct { FUNCPTR entry; /* 函数入口*/ void *arg[10]; /* 參数*/ }FUNC; void *start(void *arg) { FUNC *func = NULL; (*(func->entry))(func->arg[0], func->arg[1], func->arg[2], func->arg[3], func->arg[4], func->arg[5], func->arg[6], func->arg[7], func->arg[8], func->arg[9]); return NULL; } int PTHREAD_CTREATE(pthread_t *tidp,const pthread_attr_t *attr, void *(*funcptr)(void *, void *, void *), unsigned args, ...) { /* 构造函数集合的结构体 */ FUNC function = {0}; function.entry = funcptr; for (i = 0; i < args; i++) { func->arg[i] = arg[i]; } //设置线程属性(略) pthread_create(NULL, &attr, start, (void *)&function); return 0; }