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

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值