今天在编写多线程程序的时候,编译过程中出现了如下错误:
thread.c: In function ‘main’:
thread.c:38:57: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
后来google了,受这个问题解决的启发http://stackoverflow.com/questions/9251102/warning-cast-to-pointer-from-integer-of-different-size-wint-to-pointer-cast,找到了解决方法
出错的代码:
30 int no,res;
......
35 for(no=0;no<THREAD_NUMBER;no++)
36 {
37 /*创建多线程*/
38 res=pthread_create(&thread[no],NULL,(void *)thrd_func,(void*)no);
39 if(res!=0)
40 {
41 printf("Create thread %d failed\n",no);
42 exit(res);
43 }
44 }
将38行的(void*)no,修改成&no,就可以了,如下修改后的代码
30 int no,res;
......
35 for(no=0;no<THREAD_NUMBER;no++)
36 {
37 /*创建多线程*/
38 res=pthread_create(&thread[no],NULL,(void *)thrd_func,&no);
39 if(res!=0)
40 {
41 printf("Create thread %d failed\n",no);
42 exit(res);
43 }
44 }
再次编译就没有错误了
本文介绍了一个在使用pthread_create创建线程时遇到的警告问题:从不同大小的整数到指针的转换。通过简单的代码调整,即将传递的整数变量直接转换为指针的方式改为传递该变量地址的方式,成功解决了这一编译警告。
1044

被折叠的 条评论
为什么被折叠?



