My code
#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);
}
when compling the code, there're two warnings:
join.c:44: warning: cast from pointer to integer of different size
join.c:51: warning: cast from pointer to integer of different size
I read the man page of 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.
According to my understanding, the exist status(int type) is stored into tret
in my code, so I use (int)
to convert void *
to int
, but the above warnings are thrown out, So, my question is:
How to modify my code to clear up the warnings?