今天在编写多线程程序的时候,编译过程中出现了如下错误:
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 }
再次编译就没有错误了