线程字符串传递方式

|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| //扩展作业--参数传递 //思路: 当子线程退出的时候,pthread_exit((void *)88)--->传递给主线程 pthread_join(tid,(void *)&ret1) //函数---参数 #include <pthread.h> #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <string.h> void *task1(void *arg) //子线程1的任务 { int task1_arg = *(int *)arg; sleep(3); printf("task1 comming\r\n"); printf("arg = %d\r\n",task1_arg); pthread_exit((void *)88); //pthread_exit(void *retval); } void *task2(void *arg) //子线程2的任务 { int task2_arg = *(int *)arg; printf("task2 comming\r\n"); printf("arg = %d\r\n",task2_arg); //pthread_exit((void *)100); pthread_exit("hello world!\n"); } int main(void) { int task1_arg = 6, task2_arg = 8; int ret1; int ret2; void *thread_rs; //创建对应的线程 pthread_t tid1 , tid2; pthread_create(&tid1,NULL,task1,&task1_arg); pthread_create(&tid2,NULL,task2,&task2_arg); //作用:等待对应的子进程运行结束pthread_join(pthread_t thread,void **retval) pthread_join(tid1,(void *)&ret1); printf("ret1 = %d\r\n",(int)ret1); //pthread_join(tid2,(void *)&ret2); //printf("ret2 = %d\r\n",(int)ret2); pthread_join(tid2,&thread_rs); printf("thread finished value:%s\n",(char *)thread_rs); sleep(1); exit(0); } |

相关推荐
体系结构论文研讨会16 分钟前
多项式环及Rq的含义
算法
智驱力人工智能24 分钟前
极端高温下的智慧出行:危险检测与救援
人工智能·算法·安全·行为识别·智能巡航·高温预警·高温监测
森焱森33 分钟前
60 美元玩转 Li-Fi —— 开源 OpenVLC 平台入门(附 BeagleBone Black 驱动简单解析)
c语言·单片机·算法·架构·开源
课堂剪切板1 小时前
ch07 题解
算法·深度优先
科大饭桶3 小时前
数据结构自学Day5--链表知识总结
数据结构·算法·leetcode·链表·c
我爱C编程5 小时前
基于Qlearning强化学习的1DoF机械臂运动控制系统matlab仿真
算法
chao_7895 小时前
CSS表达式——下篇【selenium】
css·python·selenium·算法
chao_7895 小时前
Selenium 自动化实战技巧【selenium】
自动化测试·selenium·算法·自动化
YuTaoShao5 小时前
【LeetCode 热题 100】24. 两两交换链表中的节点——(解法一)迭代+哨兵
java·算法·leetcode·链表
怀旧,5 小时前
【数据结构】8. 二叉树
c语言·数据结构·算法