int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
我试图通过pthread_create将参数作为指针传递。
typedef struct {
int input;
int threadNum;
} ARGS;
ARGS argument16[16];
for (x = 0; x < 16; x++) {
printf("pthread16_created: %d\n", x);
argument16[x].threadNum = 16;
argument16[x].input = x % 4;
pthread_create(&(threads16[x]), NULL, funct, argument16[x]);
}
这给了我错误:'pthread_create'的参数4的类型不兼容
如何在ARGS数组中传递ARG类型的指针?我似乎无法弄清楚。我知道数组已经是一个指针,但是不确定如何指向数组中的某个元素。谢谢
to的最后一个参数pthread_create()
是指向void的指针,因此应将地址转换void *
为预期的地址。因此,就像评论者所说的那样,argument16 [x]的地址而不是您传递的值。否则称为右值。
在线程函数内部,您需要一个正确类型的指针,以便:
ARGS * ptr = ( ARGS * ) arg;
是回退的预期方式。
如果您不确定将来是否使用指向相同类型的指针,并在每个循环中将其递增,然后传递左值。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句