#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
void* thread_function(void *ignoredInThisExample)
{
char *a = malloc(20);
strcpy(a,"hello world");
}
int main()
{
pthread_t thread_id;
char *b;
pthread_create (&thread_id, NULL,&thread_function, NULL);
pthread_join(thread_id,(void**)&b); //here we are reciving one pointer
//value so to use that we need double pointer
printf("b is %s.\n",b);
return 0;
}
using -O0
and -O1
to compile and run:
[root c++]#gcc -g -O0 -o pthread pthread.c -lpthread
[root c++]#
[root c++]#
[root c++]#./pthread
b is hello world.
[root c++]#
[root c++]#
[root c++]#gcc -g -O1 -o pthread pthread.c -lpthread
[root c++]#
[root c++]#./pthread
b is .
why does this happen? source code from pthread_join() and pthread_exit() Since I am not familiar with assembly language, can anyone help me to analyze the reason ? online assembly