多线程(1)
创始人
2024-09-26 16:20:37
0

1、wait 函数

宏定义:WIFEXITED()    WEXITSTATUS()

调用进程 一般不做额外的事情 

#include  #include  #include  #include  #include   int main(int argc, const char *argv[]) { 	pid_t pid = fork();  	if (-1 == pid) 	{ 		perror("fork fail"); 		return -1; 	} 	if (pid > 0) 	{ 		int status; 		printf("father ----\n"); 		wait(&status); 		printf("status = %d\n",status);  		if (WIFEXITED(status)) 		{ 			printf("status = %d\n",WEXITSTATUS(status)); 		}  	}else if (pid == 0) 	{ 		printf("child exit \n"); 		exit(256); 	}	 	return 0; }

2、waitpid 函数

调用进程 逻辑一般不受影响

注意:waitpid 想要处理到子进程必须套在循环中 

 

3、pthread_库函数 

 (1)创建线程

 (2)获取线程的tid

(3)pthread_join函数:等待线程结束 

创建结束线程 

#include  #include  #include  #include   void * do_something(void *arg) { 	static int a = 10; 	//线程的任务函数  	printf("------do_something ---- \n");  	//pthread_exit(NULL); 	pthread_exit(&a); 	//pthread_exit((void *)a); }  int main(int argc, const char *argv[]) { 	pthread_t tid; 	int ret = pthread_create(&tid,NULL,do_something,NULL);  	if (ret != 0) 	{ 		errno = ret; 		perror("pthread_create fail"); 		return -1; 	}  	//sleep(3);    void *retval;  	//pthread_join(tid,&retval); 	pthread_join(tid,&retval);   	printf("---- main----%d\n",*(int *)retval); //	printf("---- main----%d\n",(int)retval);  	pthread_exit(NULL);  	return 0; }
#include  #include  #include  #include   void * do_something1(void *arg) { 	int *p = arg; 	//((int)(*arg))+=1; 	int a = *p; 	while (1) 	{ 		//线程的任务函数  		printf("------do_something1 ---- tid = %ld a = %d\n",pthread_self(),a++); 		sleep(1); 	} }  void * do_something2(void *arg) { 	int *p = arg;    //线程的任务函数     int a = *p;    while (1)    { 	   a += 2; 		printf("------do_something2 ---- tid = %ld a = %d\n",pthread_self(),a); 		sleep(1);    } }  int main(int argc, const char *argv[]) { 	int a = 10;  	pthread_t tid1; 	int ret = pthread_create(&tid1,NULL,do_something1,&a);  	if (ret != 0) 	{ 		errno = ret; 		perror("pthread_create fail"); 		return -1; 	}  	pthread_t tid2; 	ret = pthread_create(&tid2,NULL,do_something2,&a );  	if (ret != 0) 	{ 		errno = ret; 		perror("pthread_create fail"); 		return -1; 	}     //main do something   	while (1) 	{ 		printf("----main-----do_something--tid = %ld-\n",pthread_self()); 		sleep(1); 	}   	sleep(3); 	 	return 0; }

相关内容

热门资讯

据相关数据显示!约局吧怎么看有... 据相关数据显示!约局吧怎么看有没有挂,wepoker破解器有用吗,一贯存在有挂(有挂方法)1、进入到...
2026版攻略!wepoker... 2026版攻略!wepoker透视版下载,aapoker怎么设置提高好牌几率,原来确实有挂(有挂详情...
昨日!werplan外挂,aa... 昨日!werplan外挂,aapoker辅助器怎么用,真是确实有挂(了解有挂)1、不需要AI权限,帮...
第三方辅助挂!wpk插件,aa... 第三方辅助挂!wpk插件,aapoker公共底牌,切实真的有挂(有挂分享)1、aapoker公共底牌...
2026版教程!德扑圈透视,p... 2026版教程!德扑圈透视,pokemmo辅助工具,一贯确实有挂(详细教程)1、pokemmo辅助工...
为了进一步!wpk俱乐部怎么作... 为了进一步!wpk俱乐部怎么作弊,wepoker透视器免费,一直是有挂(有挂方法);1、为了进一步!...
这一现象值得深思!wepoke... 这一现象值得深思!wepoker高级辅助,wepoker怎么发冤家牌,竟然是有挂(有挂透明挂)wep...
方法辅助挂!哈糖大菠萝辅助器,... 方法辅助挂!哈糖大菠萝辅助器,hhpoker德州挂真的有吗,切实真的有挂(有挂教程)1、起透看视 h...
不少玩家反映!hhpoker可... 不少玩家反映!hhpoker可以控制吗,wepoker私人局怎么玩,一贯真的是有挂(有挂秘笈)1、不...
据悉!德普之星app安卓版破解... 据悉!德普之星app安卓版破解版,wpk显示有作弊,其实是有挂(果真有挂)德普之星app安卓版破解版...