Linux- 线程函数如何将返回值传给主线程
来源:互联网 发布:contabs.js插件 编辑:程序博客网 时间:2024/05/17 22:24
网上找到几种方法,一一记录。
1. 定义一个 包含 线程函数的 参数和返回值的 数据结构。
例子如下:
#include <pthread.h>#include <stdio.h>typedef struct thread_data { int a; int b; int result;} thread_data;void *myThread(void *arg){ thread_data *tdata=(thread_data *)arg; int a=tdata->a; int b=tdata->b; int result=a+b; tdata->result=result; pthread_exit(NULL);}int main(){ pthread_t tid; thread_data tdata; tdata.a=10; tdata.b=32; pthread_create(&tid, NULL, myThread, (void *)&tdata); pthread_join(tid, NULL); printf("%d + %d = %d\n", tdata.a, tdata.b, tdata.result); return 0;}
例子如下:
#include <pthread.h>#include <stdio.h>int something_worked(void) { /* thread operation might fail, so here's a silly example */ void *p = malloc(10); free(p); return p ? 1 : 0;}void *myThread(void *result){ if (something_worked()) { *((int*)result) = 42; pthread_exit(result); } else { pthread_exit(0); }}int main(){ pthread_t tid; void *status = 0; int result; pthread_create(&tid, NULL, myThread, &result); pthread_join(tid, &status); if (status != 0) { printf("%d\n",result); } else { printf("thread failed\n"); } return 0;}
3. 另外,我觉得还可以通过全局变量来实现子线程和主线程的信息传递,但是多线程使用全局变量的时候,要注意线程同步的包含。
下面这段话说的蛮好:
Question : What is the best practice of returning/storing variables of multiple threads? A global hash table?
This totally depends on what you want to return and how you would use it? If you want to return only status of the thread (say whether the thread completed what it intended to do) then just use pthread_exit or use a return statement to return the value from the thread function.
But, if you want some more information which will be used for further processing thenyou can use global data structure. But, in that case you need to handle concurrency issues by using appropriate synchronization primitives.Or you can allocate some dynamic memory (preferrably for the structure in which you want to store the data) and send it via pthread_exit and once the thread joins, you update it in another global structure. In this way only the one main thread will update the global structure and concurrency issues are resolved. But, you need to make sure to free all the memory allocated by different threads.
- Linux- 线程函数如何将返回值传给主线程
- Android 将信息传给主线程处理
- 如何将后台返回的值传给回调函数之外的变量
- 子线程结束后返回值给主线程
- 主线程使用WaitForSingleObject和MsgWaitForMultipleObjects等待线程返回值
- 如何将二维数组作为参数传给一个函数
- handler底层是如何将消息从子线程发送到主线程
- 将一个函数在主线程执行及子线程执行的方法
- 自己的主线程将其它所有线程中的当前函数调用栈信息打印出来
- 如何将子窗体的值传给父窗体
- 在.bat脚本中如何将一个命令的返回结果传给一个变量?
- Handler: 主线程如何通知子线程
- Handler: 主线程如何通知子线程
- Handler: 主线程如何通知子线程
- Handler: 主线程如何通知子线程
- Handler: 主线程如何通知子线程
- 主线程 如何控制 子线程
- 在UI主线程里调用带参数和返回值的线程
- PKI技术原理
- android 解决两个应用互相跳转,如果应用已经启动还是会重新打开应用的问题
- thinkphp之category tree
- JFlow工作流 流程与表单案例
- Java遍历Map的四种方法
- Linux- 线程函数如何将返回值传给主线程
- selenium2.x python 虚拟浏览器 搭建与使用
- Redhat6.2安装thrift0.9.1
- c# Eval函数中将日期显示的格式修改为yyyy-MM-dd形式
- 信息学奥林匹克竞赛-校门外的树
- cocos2dx-lua方法笔记
- 在Ubuntu中遇到Unable to lock the administration directory (/var/lib/dpkg/),are you root? 问题解决办法
- 关于UniversalImageLoader加载图片时的闪烁问题
- 深入分析:Android中app之间的交互(二,使用ComponentName)