我一直在学习pthread库,并且已经创建了一个简单的代码来相乘两个数字,但是我无法摆脱这个警告。附注:代码运行良好。
#include <stdio.h>
#include <pthread.h>
struct numbers {
int num1,num2;
};
void *mult(void *param) {
struct numbers *data = param;
int res = data->num1 * data->num2;
pthread_exit((void *)res);
}
int main(){
pthread_t tid;
struct numbers n;
n.num1 = 2;
n.num2 = 3;
pthread_create(&tid, NULL,mult, (void*)&n);
int res;
pthread_join(tid, (void *)&res);
printf("%d\n",(int)res);
return 0;
}
警告如下:
1.c: In function ‘mult’:
1.c:12:17: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
12 | pthread_exit((void *)res);
如有任何真知灼见,将不胜感激。
更改
pthread_exit((void *)res);
至
pthread_exit((void *)&res);
确保状态变量res
在线程退出后可以访问。如果不需要线程退出状态,最好传递nullptr