pthread_exit & pthread_join 有一种被骗了的感觉!

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <pthread.h>

 

 

int *exitStatus = NULL;

int count = 0;

 

void* run_up(void *arg)

{

printf("&exitStatus: %d, exitStatus: %d, *exitStatus: %d, &*exitStatus: %d\n", &exitStatus, exitStatus, *exitStatus, &*exitStatus);

 

pthread_exit((void *) exitStatus);

//pthread_exit(NULL);

//const char *s = "Thread funtion finished!";

//pthread_exit((void *) s);

 

//return (void *) exitStatus;

//return NULL;

//return (void *) "Thread funtion finished!";

}

 

 

int main()

{

 

exitStatus = (int *) malloc(sizeof(int));

*exitStatus = 8;

 

pthread_t pthread1, pthread2;

pthread_create(&pthread1, 

NULL, 

run_up, 

(void *) 0);

 

int *p_status = (int *) malloc(sizeof(int));

int **status = &p_status;

pthread_join(pthread1, (void **) status);

printf("&p_status: %d, p_status: %d, *p_status: %d, &*p_status: %d\n", &p_status, p_status, *p_status, &*p_status);

printf("&status: %d, status: %d, *status: %d, &*status: %d, **status: %d, &**status: %d\n", &status, status, *status, &*status, **status, &**status);

 

printf("final count: %d\n", count);

return 0;

}

 

================================

 

OUTPUT = ../Debug/

PosixThreadJoinTest_objects = $(OUTPUT)*.o

Release: clean

g++ -c PosixThreadJoinTest.cpp -o ../Debug/PosixThreadJoinTest.o

g++ $(PosixThreadJoinTest_objects) -o $(OUTPUT)PosixThreadJoinTest -L. -lpthreadGC2

#g++ $(PosixThreadJoinTest_objects) -o $(OUTPUT)PosixThreadJoinTest -lpthread

 

clean:

rm -Rf ./*.bak

rm -Rf ./*.o

rm -Rf ./*.exe

rm -Rf ../Debug/*

 

运行输出:

 

&p_status: 2280728, p_status: 5614848, *p_status: 0, &*p_status: 5614848

&exitStatus: 4210716, exitStatus: 5614832, *exitStatus: 8, &*exitStatus: 5614832

&status: 2280724, status: 2280728, *status: 5614848, &*status: 2280728, **status: 0, &**status: 5614848

final count: 0

 

==============================================

 

OUTPUT = ../Debug/

PosixThreadJoinTest_objects = $(OUTPUT)*.o

Release: clean

g++ -c PosixThreadJoinTest.cpp -o ../Debug/PosixThreadJoinTest.o

#g++ $(PosixThreadJoinTest_objects) -o $(OUTPUT)PosixThreadJoinTest -L. -lpthreadGC2

g++ $(PosixThreadJoinTest_objects) -o $(OUTPUT)PosixThreadJoinTest -lpthread

 

clean:

rm -Rf ./*.bak

rm -Rf ./*.o

rm -Rf ./*.exe

rm -Rf ../Debug/*

 

运行输出:

 

&exitStatus: 4210716, exitStatus: 5221616, *exitStatus: 8, &*exitStatus: 5221616

&p_status: 2280728, p_status: 5221616, *p_status: 8, &*p_status: 5221616

&status: 2280724, status: 2280728, *status: 5221616, &*status: 2280728, **status: 8, &**status: 5221616

final count: 0

 

 

 

你可能感兴趣的:(pthread)