线程字符串传递方式

|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| //扩展作业--参数传递 //思路: 当子线程退出的时候,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); } |

相关推荐
深邃-18 小时前
【数据结构与算法】-二叉树(2):实现顺序结构二叉树(堆的实现),向上调整算法,向下调整算法,堆排序,TOP-K问题
数据结构·算法·二叉树·排序算法·堆排序··top-k
We་ct21 小时前
LeetCode 5. 最长回文子串:DP + 中心扩展
前端·javascript·算法·leetcode·typescript
王老师青少年编程1 天前
csp信奥赛C++高频考点专项训练之贪心算法 --【哈夫曼贪心】:合并果子
c++·算法·贪心·csp·信奥赛·哈夫曼贪心·合并果子
叼烟扛炮1 天前
C++第二讲:类和对象(上)
数据结构·c++·算法·类和对象·struct·实例化
天疆说1 天前
【哈密顿力学】深入解读航天器交会最优控制中的Hamilton函数
人工智能·算法·机器学习
wuweijianlove1 天前
关于算法设计中的代价函数优化与约束求解的技术7
算法
leoufung1 天前
LeetCode 149: Max Points on a Line - 解题思路详解
算法·leetcode·职场和发展
样例过了就是过了1 天前
LeetCode热题100 最长公共子序列
c++·算法·leetcode·动态规划
HXDGCL1 天前
矩形环形导轨:自动化循环线的核心运动单元解析
运维·算法·自动化
谭欣辰1 天前
C++ 排列组合完整指南
开发语言·c++·算法