Linux 下子线程 exit code 在主线程中的使用
Linux线程函数原型是这样的:
void* thread_fun(void* arg)
它的返回值是 空类型指针,入口参数也是 空类型指针。那么线程的 exit code 也应该是 void * 类型的。但是在主线程怎么捕获子线程的 exit code 并使用的呢?
捕获的思路如下:
1. 在主线程中定义一个 void* tret;
2. 使用 pthread_join(tidxx, &tret);
这样就能够捕获到子线程的 exit code。
但是如何使用呢?这就取决于子线程中的 exit code 具体表示的数据类型了,可以是 int, char *, struct xxxStr 等等,然后直接在主线程中使用类型转换到对应的数据类型就行了。
例如:
/****************************************************************
# File Name: thread_cleanup2.c
# Author : lintex9527
# E-Mail : lintex9527@yeah.net
# Created Time: Sat 22 Aug 2015 11:01:59 AM HKT
# Purpose :
# Outline :
# Usage :
# --------------------------------------------------
# Result :
# --------------------------------------------------
*****************************************************************/ #include <stdio.h>
#include <stdlib.h>
#include <pthread.h> void* thr_fn01(void *arg)
{
printf("thread 1 start:\n");
pthread_exit((void *));
pthread_exit((void *)"SOT-26"); // the first pthread_exit() works, the rest below that does not execute.
} void* thr_fn02(void *arg)
{
printf("thread 2 start:\n");
pthread_exit((void *)"SOT-363");
} int main(void)
{
int err;
pthread_t tid1, tid2;
void *tret; pthread_create(&tid1, NULL, thr_fn01, (void *));
pthread_join(tid1, &tret);
printf("thread 1 exit code: %d\n", (tret));
printf("thread 1 exit code: %d\n", (int *)(tret)); pthread_create(&tid2, NULL, thr_fn02, (void *));
pthread_join(tid2, &tret);
printf("thread 2 exit code: %s\n", (tret));
printf("thread 2 exit code: %s\n", (char *)(tret)); return ;
}
执行的结果如下:
$ ./thread_cleanup2.exe
thread start:
thread exit code:
thread exit code:
thread start:
thread exit code: SOT-
thread exit code: SOT-
可以看到“直接使用指针方式” 和 “强制类型转换方式” 输出的结果都一样。
上面的都是基本数据类型方式,那么再试一下其他的数据类型,例如结构体:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h> struct personStr{
char name[];
unsigned int age;
char sex; // 'M', 'W'
char ID[];
}; void printPerson(const char * str, struct personStr *p)
{
printf("%s\n", str);
printf(" name:%s\n", p->name);
printf(" age: %d\n", p->age);
printf(" sex: %c\n", p->sex);
printf(" ID: %s\n", p->ID);
} struct personStr thisman; void* thr_fn01(void *arg)
{
printf("thread 1 start:\n");
memcpy(thisman.name, "Lee", strlen("Lee"));
thisman.age = ;
thisman.sex = 'M';
memcpy(thisman.ID, "421127xxxx78455623", strlen("421127xxxx78455623"));
printPerson("In pthread 1:", &thisman); pthread_exit((void *)&thisman);
} void* thr_fn02(void *arg)
{
printf("thread 2 start:\n");
pthread_exit((void *)"SOT-363"); } int main(void)
{
int err;
pthread_t tid1, tid2;
void *tret; err = pthread_create(&tid1, NULL, thr_fn01, (void *));
pthread_join(tid1, &tret);
printPerson("In main thread:", tret); // 直接使用指针
printPerson("In main thread:", (struct personStr *)tret); // 强制类型转换为结构体指针 err = pthread_create(&tid2, NULL, thr_fn02, (void *));
pthread_join(tid2, &tret);
printf("thread 2 exit code: %s\n", (tret));
printf("thread 2 exit code: %s\n", (char *)(tret)); return ;
}
执行结果如下:
$ ./thread_cleanup2.exe
thread start:
In pthread :
name:Lee
age:
sex: M
ID: 421127xxxx78455623
In main thread:
name:Lee
age:
sex: M
ID: 421127xxxx78455623
In main thread:
name:Lee
age:
sex: M
ID: 421127xxxx78455623
thread start:
thread exit code: SOT-
thread exit code: SOT-
可以看到 “直接使用指针” 和 “强制类型转换”结果都是一样的。如果图方便就直接使用指针,而且这样的代码通用性也好,万一将来某天结构体名字变动了,就需要改动很多地方了,但是也有弊病,就是代码的易读性不好。
有一点奇怪的就是第一个例子中,为什么返回的是整数 int 类型的"100",却能通过指针打印出"100"呢?
这样验证:
printf("thread 1 exit code: %d\nsizeof tret is %d Bytes\nsizeof(int) is %d Bytes.\n", (tret), sizeof(tret), sizeof(int));
结果是这样的:
thread exit code:
sizeof tret is Bytes
sizeof(int) is Bytes.
那么就说明 tret 的确是指针类型的,占用了8个字节的数据,而 int 类型的数据只占用了4个字节,而且进行如下的尝试,编译失败了:
printf("tret * 3 = %d\n", tret * );
很不幸,失败了,结果:
thread_cleanup2.c:: error: invalid operands to binary * (have ‘void *’ and ‘int’)
make: *** [thread_cleanup2.o] Error
如果的确是想使用 tret 的值100, 可否通过指针取值运算呢?
printf("tret * 3 = %d\n", (*((int *)tret)) * );
很不幸,这样也失败了。
如果要想把返回值 tret 参与其他的运算,就必须使用一个转换的桥梁。利用 “中间变量 = tret”,然后使用这个中间变量,虽然编译会有 warning 提醒,但是的确能使用:
//printf("tret * 3 = %d\n", (*((int *)tret)) * 3);// failed.
//int num = *((int *)tret); // failed.
int num = tret;
printf("num = %d, num * 3 = %d\n", num, num * );
编译提示:
thread_cleanup2.c:95: warning: initialization makes integer from pointer without a cast
cc -o thread_cleanup2.exe thread_cleanup2.o -lpthread
运行结果:
thread 1 exit code: 100
sizeof tret is 8 Bytes
sizeof(int) is 4 Bytes.
num = 100, num * 3 = 30
2015-08-22 13:17:12 于公司。
Linux 下子线程 exit code 在主线程中的使用的更多相关文章
- 开子线程下载图片,回到主线程刷新UI步骤
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { [NSThread detachN ...
- 用Handler的post()方法来传递线程中的代码段到主线程中执行
自定义的线程中是不能更新UI的,但是如果遇到更新UI的事情,我们可以用handler的post()方法来将更新UI的方法体,直接传送到主线程中,这样就能直接更新UI了.Handler的post()方法 ...
- android4.0以上访问网络不能在主线程中进行以及在线程中操作UI的解决方法
MONO 调用一个线程操作UI 然后报Only the original thread that created a view hierarchy can touch its views.错误 goo ...
- Android中,子线程使用主线程中的组件出现问题的解决方法
Android中,主线程中的组件,不能被子线程调用,否则就会出现异常. 这里所使用的方法就是利用Handler类中的Callback(),接受线程中的Message类发来的消息,然后把所要在线程中执行 ...
- Java线程和多线程(四)——主线程中的异常
作为Java的开发者,在运行程序的时候会碰到主线程抛异常的情况.如果开发者使用Java的IDE比如Eclipse或者Intellij IDEA的话,可能是不需要直接面对这个问提的,因为IDE会处理运行 ...
- [原]unity中WWW isDone方法只能在主线程中调用
项目中要使用动态加载,原计划是生成WWW对象后,放到一个容器里.由一个独立线程轮询容器里的对象,如果www.isDone为true时,回调一个接口把结果交给请求方. new Thread( new T ...
- 主线程中也不绝对安全的 UI 操作
从最初开始学习 iOS 的时候,我们就被告知 UI 操作一定要放在主线程进行.这是因为 UIKit 的方法不是线程安全的,保证线程安全需要极大的开销.那么问题来了,在主线程中进行 UI 操作一定是安全 ...
- 在主线程中慎用WaitForSingleObject (WaitForMultipleObjects)
下面的代码我调试了将近一个星期,你能够看出什么地方出了问题吗?线程函数: DWORD WINAPI ThreadProc( while(!bTerminate) { // 从 ...
- httpUrlConnection连接网络的用法(用到了handle传递消息,在主线程中更新UI)
由于httpclient在Android5.0以后已经过时,所以官方推荐使用httpUrlConnection来连接网络,现将该连接的基本方法展示,如下 注意:记得加入<uses-permiss ...
随机推荐
- C#简单文件下载-3行代码
使用WebClient string url = "http://www.mozilla.org/images/feature-back-cnet.png"; WebClient ...
- Python入门笔记(9):元组
一.元组特性 1.类似列表,但不可变类型,正因如此,它可以做一个字典的key2.当处理一组对象时,这个组默认是元组类型(老写错"元祖")3.所有的多对象,逗号分隔的,没有明确用符号 ...
- Python基础:开篇
一.概述 Python基础 是对 Python(2.7版本)基本语法的要点总结,主要参考了以下资料: Python核心编程(第二版) Learning Python (3rd Edition) Pyt ...
- 重新想象 Windows 8 Store Apps (34) - 通知: Toast Demo, Tile Demo, Badge Demo
[源码下载] 重新想象 Windows 8 Store Apps (34) - 通知: Toast Demo, Tile Demo, Badge Demo 作者:webabcd 介绍重新想象 Wind ...
- Linux服务器时间同步方法
一般稍微大点的项目都会部署到好几台服务器做集群,同一个应用可能部署到几台服务器上,而处理业务中必须让不同的服务器上时间保持一致,这就需要进行服务器间的时间同步.我的做法是: 1,选择其中一台对外网开放 ...
- jsp页面 列表 展示 ajax异步实现
1. 服务端先返回页面基本结构(如message.jsp), <%@ page language="java" contentType="text/html; ch ...
- linux下socket编程
相关结构 //下边这两个结构定义在<sys/types.h>里 //一般的地址结构,只能用于覆盖(把其他地址转换为此类型),且只能引用该地址的sa_family字段 struct sock ...
- ADO.NET 完整的修改和删除
namespace 完整修改{ class Program { static void Main(string[] args) { bool has = false; Console.Write(&q ...
- osx的终端软件iterm2 之 修改外观 和 常用快捷键小结
1.修改外观:透明,自己配色,最好还有个透明的小背景,比如这样: 那么你就要这样修改: 2.快捷键小结 (1)⌘ + d 横着分屏 / ⌘ + shift + d 竖着分屏 : 适合多操作的时候 ( ...
- jQuery淡入淡出效果轮播图
用JavaScript做了平滑切换的焦点轮播图之后,用jQuery写了个简单的淡入淡出的轮播图,代码没有做优化,html结构稍微有一些调整,图片部分用ul替换了之前用的div. html结构如下: & ...