我的代码
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
void * thread_func1(void *args)
{
printf("thread 1 returning\n");
return ((void *)1);
}
void * thread_func2(void *args)
{
printf("thread 2 exiting\n");
pthread_exit((void *)2);
}
int main(void)
{
int err;
pthread_t tid1, tid2;
void *tret;
err = pthread_create(&tid1, NULL, thread_func1, NULL);
if(err)
{
printf("cannot create thread 1\n");
}
err = pthread_create(&tid2, NULL, thread_func2, NULL);
if(err)
{
printf("cannot create thread 2\n");
}
err = pthread_join(tid1, &tret);
if(err)
{
printf("thread 1 join error\n");
}
printf("thread 1 return code:%d\n", (int)tret);
err = pthread_join(tid2, &tret);
if(err)
{
printf("cannot join thread 2\n");
}
printf("thread 2 return code:%d\n", (int)tret);
exit(0);
}
编译代码时,有两个警告:
join.c:44: warning: cast from pointer to integer of different size
join.c:51: warning: cast from pointer to integer of different size
我阅读了以下的手册页pthread_join
:
int pthread_join(pthread_t thread, void **retval);
If retval is not NULL, then pthread_join() copies the exit status of
the target thread (i.e., the value that the target thread supplied to
pthread_exit(3)) into the location pointed to by *retval. If the
target thread was canceled, then PTHREAD_CANCELED is placed in
*retval.
根据我的理解,存在状态(int类型)存储tret
在我的代码中,所以我使用(int)
转换void *
为int
,但是上面的警告被抛出,所以,我的问题是:
如何修改我的代码以清除警告?