线程字符串传递方式

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

相关推荐
橘子139 小时前
【高级数据库】关系代数基本概念
算法
电科_银尘20 小时前
【大模型算法与工程 15】手写Self-Attention:Q、K、V与缩放点积
算法
郝学胜-神的一滴1 天前
Effective STL 条款03:容器中的对象复制要廉价且正确
开发语言·数据结构·c++·算法·rpc·stl
秋天的怀念_David1 天前
算法一千零一夜 · 第三十三夜 · 跳表
java·程序人生·算法
Pniubi1 天前
力扣21合并两个有序链表
算法·leetcode·职场和发展
AIgorithmGEEK1 天前
std::unordered_map 与 std::unordered_set:接口、算法技巧与权衡 (速查)
c++·算法·哈希表·负载因子·桶·unordered_map·unordered_set
上海潭龙东海科技有限公司1 天前
智慧堆场动态模型三维实时孪生重构智能管控
大数据·网络·数据库·算法·重构
阿庆_AI研发工程师1 天前
Stanford CS336 Lecture 10:大语言模型推理与性能优化
算法
用户7341681035481 天前
基于MindSpore的深度残差收缩网络程序
算法
Pniubi1 天前
力扣239.滑动窗口最大值(优先队列)
算法·leetcode·职场和发展