linux C 主線成與子線程參數傳遞

#include <stdio.h>
#include <pthread.h>

void* fun(void* arg)
{
 
 
 printf("======[%d]====\n",(int)arg);  
 pthread_exit((void*)22);
}

int main(void)
{

 pthread_t tid; 

 pthread_create(&tid,NULL,fun,(void*)99);    //你沒注意的東東
 void *val;
 pthread_join(tid,&val);
 printf("------[%d]-----\n",(int)val);


 return 0;
}

第二種傳結構體    
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>

typedef struct 
{
	int a;
	float b;
	
}*LYp,LY;

LYp sheep;
float f=0;
void* fun(void* arg)
{
	LYp p=(LYp)arg;
	printf("===[%d]===[%f]====\n",p->a,p->b);
	f=1.234;		
	pthread_exit(&f);
}

int main(void)
{
	sheep=malloc(sizeof(LY));
	sheep->a=100;
	sheep->b=3.33;
	pthread_t tid;	

	pthread_create(&tid,NULL,fun,sheep);
	void *val;
	pthread_join(tid,&val);
	printf("------[%lf]-----\n",*((float*)val));

	free(sheep);


	return 0;
}

//第三種 通過全局變量傳遞好用
 



 

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