Linux編程:線程終止、線程掛起(類似進程的wait)函數使用

Linux編程:線程終止、線程掛起(類似進程的wait)函數使用

#include<stdlib.h>
#include<stdio.h>
#include<unistd.h>
#include<pthread.h>
typedef struct{
	int a;
	int b;
}exit_t;
void *tfn(void *arg)
{
	exit_t *ret;
	ret=malloc(sizeof(exit_t));
	ret->a=100;
	ret->b=200;
	pthread_exit((void *)ret);//線程終止//參數可以爲NULL
	return NULL;//線程返回
}
int main(void)
{
	pthread_t tid;
	exit_t * retval;
	pthread_create(&tid,NULL,tfn,NULL);
	//調用pthread_join獲取線程退出狀態
	pthread_join(tid,(void**)&retval);
	printf("a=%d,b=%d\n",retval->a,retval->b);
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章