LinuxDev 板


LINE

: → hpeter:pthread_join & pthread_exit 03/18 00:12 : → psboy:不过问题又来了 如果是回传一堆资料怎麽办?传址似乎会出鎚 03/18 11:27 : → psboy:原来一开始就传struct进去在thread里面设定好回传值就好了xD 03/18 16:49 : → hpeter:可以试试在 thread 里 malloc 一个struct 用 pthread_exit 03/18 20:02 : → hpeter:回传 address XD 03/18 20:02 试着用malloc一个struct的方法可行 :D 以下是乱改的code xD #include <stdio.h> #include <pthread.h> struct st { int a; char * b; }; void * thread1(void * in) { char * s=(char *)in; printf("This is a pthread1.\n"); printf("%s\n",s); struct st * ast; ast=malloc(sizeof(struct st)); ast->a=111; ast->b="thread1 hello!"; pthread_exit(ast); } void * thread2(void * in) { char * s=(char *) in; printf("This is a pthread2.\n"); printf("%s\n",s); struct st * bnd; bnd=malloc(sizeof(struct st)); bnd->a=222; bnd->b="thread2 hello!"; pthread_exit(bnd); } /**************main function ****************/ int main(void) { pthread_t id1,id2; void *a1,*a2; int i,ret1,ret2; char s1[]="This is first thread!"; char s2[]="This is second thread!"; ret1=pthread_create(&id1,NULL,(void *) thread1,s1); ret2=pthread_create(&id2,NULL,(void *) thread2,s2); if(ret1!=0){ printf ("Create pthread1 error!\n"); exit (1); } pthread_join(id1,&a1); struct st * tmp; tmp=(struct st *) a1; printf("thread1 return int a:%d,string b:%s\n",tmp->a,tmp->b); if(ret2!=0){ printf ("Create pthread2 error!\n"); exit (1); } printf("This is the main process.\n"); pthread_join(id2,&a2); tmp=(struct st *) a2; printf("thread2 return int a:%d,string b:%s\n",tmp->a,tmp->b); } - 至於变数为啥是ast跟bnd 原本是用1st跟2nd compiler不给过 xD 所以才偷改一个字 Or2 也有人跟我说 一开始就传struct位址进去 似乎也是个办法 不知道用那种比较好? --



※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 219.87.143.66 ※ 编辑: psboy 来自: 219.87.143.66 (03/18 20:45)
1F:推 POSIX:st的char* b 使用时没malloc, 危险唷~ 03/19 06:32
2F:→ POSIX:这种方法"比较"不好 因为pthread_exit通常用来回报正确与否 03/19 06:32
3F:→ POSIX:但是如果你拿来用成回传值 也无不妥 只是当你要回传status 03/19 06:33
4F:→ POSIX:就要另外设计了 03/19 06:33
5F:→ psboy:那有没有比较好的作法?char * b没有malloc 那是要怎样写? 03/19 09:29
6F:推 POSIX:i mean[ast->b="thread1 hello!"] 你没malloc char array 03/19 10:38
7F:→ POSIX:danger! 03/19 10:38
8F:推 sunneo:传struct位址到in比较好 03/19 11:36







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:BabyMother站内搜寻

TOP