(void*) 4: Convert reshape 4 to address value 0x4
Today I wrote a program, I almost did not explain the pass.
Just started to write Pthread_exit (0); compile, run no problem
Written pthread_exit (4); Compile warning, run no problem.
Warning for the following reasons:
JOINTHREAD.C: In function ' Assisthread ':
jointhread.c:9:18: Warning: Assigning an integer to a pointer when passing the 1th argument of ' pthread_exit ', without type conversion [- Wint-conversion]
pthread_exit (4);
^
in file included from jointhread.c:4:0:
/usr/include/pthread.h:244:13: Note: Requires type ' void * ', but the type of the argument is ' int '
extern void Pthread_exit (void *__retval) __attribute__ ((__noreturn__));
The main problem is the type, 0 is special, can be considered null.
Pthread_exit (void *__retval) requires a variable of type void *.
Pthread_exit ((void*) 4); change it to the right way.
FILE:JOINTHREAD.C
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
void * Assisthread (void * arg) {
printf ("I am helping to do some jobs\n");
Sleep (3);
Pthread_exit ((void*) 4);
}
int main (int argc, const char *argv[])
{
pthread_t assistthid;
int status;
Pthread_create (&assistthid, NULL, (void*) assisthread, null);
Pthread_join (Assistthid, (void*) &status);
printf ("Assistthread ' exit is cased%d\n", status);
return 0;
}
Run Result:
I am helping to do some jobs
assistthread ' exit is cased 4
Here's the question: (void*) 4 What a ghost.
The first is the type conversion, which we all know. is to convert 4 to address value: (void*) 4=0x4
And here I used (void*) &status to receive this address value (0x4)
The status variable itself has an address, assumed to be 8000, and the content of the status address (8000), with an address value (0x4)
Then, when it was printed with%d, the address value was converted to an integer 4.
Write a small example of yourself to understand:
void *a;
A= (void*) 2; printf ("(void*) 2=%p, a=%d\n", (void*) 2, a);