|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| //扩展作业--参数传递 //思路: 当子线程退出的时候,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); } |
线程字符串传递方式
ST小智2023-07-29 20:01
相关推荐
幸运超级加倍~4 分钟前
软件设计师-上午题-16 算法(4-5分)yannan2019031311 分钟前
【算法】(Python)动态规划埃菲尔铁塔_CV算法13 分钟前
人工智能图像算法:开启视觉新时代的钥匙EasyCVR13 分钟前
EHOME视频平台EasyCVR视频融合平台使用OBS进行RTMP推流,WebRTC播放出现抖动、卡顿如何解决?linsa_pursuer14 分钟前
快乐数算法小芒果_0116 分钟前
P11229 [CSP-J 2024] 小木棍qq_4340859017 分钟前
Day 52 || 739. 每日温度 、 496.下一个更大元素 I 、503.下一个更大元素IIBeau_Will17 分钟前
ZISUOJ 2024算法基础公选课练习一(2)XuanRanDev20 分钟前
【每日一题】LeetCode - 三数之和gkdpjj21 分钟前
C++优选算法十 哈希表