更新时间:2022-06-27 00:50:04
定义为 int (*)(void*)
的函数与定义为 void* (*)(void*)
.您需要将 probSAT
定义为:
A function defined as int (*)(void*)
is not compatible with one defined as void* (*)(void*)
. You need to define probSAT
as:
void *probSAT(void *);
如果你想从这个函数中有效地返回一个 int
,你可以返回一个全局变量的地址或者(更好的选择)为 int
分配空间并返回一个指向它的指针(并确保在加入线程时释放它).
If you want to effectively return an int
from this function, you can either return the address of a global variable or (the better option) allocate space for an int
and return a pointer to that (and ensure you deallocate it when you join the thread).
void *probSAT(void *param) {
int *rval = malloc(sizeof(int));
if (rval == NULL) {
perror("malloc failed");
exit(1);
}
....
*rval = {some value};
return rval;
}
void get_thread_rval(pthread_t thread_id)
{
void *rval;
int *rval_int;
if (pthread_join(thread_id, &rval) != 0) {
perror("pthread_join failed");
} else {
rval_int = rval;
printf("thread returned %d\n", *rval_int);
free(rval_int);
}
}