C_and_CPP 板


LINE

开发平台(Platform): (Ex: Win10, Linux, ...) Linux 额外使用到的函数库(Library Used): (Ex: OpenGL, ...) pthread 问题(Question): 最近在做多执行绪的实作遇到两个问题 1. 因为pthread_create要呼叫的函式需要的参数是用指标宣告 所以函式的参数宣告成(void *) 我用一个args array传入参数 那这个参数在函式中该怎麽取用呢 我写的直接用arg[0], arg[1]应该是错的 compiler会有dereferencing "void *" pointer的warning 2. 另外最後我想要divide成两个部份给两个执行绪去执行 呼叫的pthread_create的第三个&第四个参数 直接给函式名字&新的存参数array这样是对的吗 错误结果(Wrong Output): dereferencing "void *" pointer的warning 程式码(Code):(请善用置底文网页, 记得排版,禁止使用图档) int Partition(int p, int r) { ... return q; } void* func(void *arg) { pthread_attr_t attr; pthread_t thread_L; pthread_t thread_R; pthread_attr_init(&attr); int p = arg[0]; int r = arg[1]; if (p < r) { int q = Partition(p, r); int arg_L[2] = { p, q-1 }; int arg_R[2] = { q+1, r }; //QuickSort(arg_L); if(pthread_create(&thread_L, NULL, func, arg_L)) { fprintf(stderr, "error an exit\n"); return NULL; } //QuickSort(arg_R); if(pthread_create(&thread_R, NULL, func, arg_R)) { fprintf(stderr, "error an exit\n"); return NULL; } pthread_join(thread_L, NULL); pthread_join(thread_R, NULL); } } int main(int argc, char *argv[]) { ... int args[2] = { p, r }; func(args); } 补充说明(Supplement): 抱歉对指标的概念不是很好,还请前辈们多多赐教,感谢万分 --



※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 36.229.214.58
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/C_and_CPP/M.1539235025.A.C96.html ※ 编辑: skyHuan (36.229.214.58), 10/11/2018 13:17:56
1F:→ descent: 你的 func() 怎麽自己又要 create thread?10/11 17:25
2F:→ descent: 是 Partition 吗?10/11 17:25
其实func()原本是Quicksort 我把跟问题没关系的地方省略了 Partition函式用来找divide的地方的 找到之後create thread分两部分Quicksort
3F:推 Ryspon: int p = (int) arg[0]10/11 17:35
所以是型别转换的问题吗 我试了呼叫时func((void*)args); 呼叫完转(int)好像都有点怪怪的 不确定是不是要像argc argv那样 函式参数的定义要改才能直接用 void* func(void *arg[]) { } 可是这样呼叫函式前要设定的参数 int args[2] = { p, r }; 我就不知道要怎麽改了 不好意思指标的观念没有很清楚弄得很混乱>< ※ 编辑: skyHuan (223.137.198.187), 10/11/2018 18:02:35 ※ 编辑: skyHuan (223.137.198.187), 10/11/2018 18:04:21
4F:推 achicn3: int p,q;改intptr_t型态 10/11 18:13
5F:→ joe820730: https://ideone.com/tQloW010/11 19:41
6F:→ joe820730: 在thread里面建一个变数把转型後的指标存起来 10/11 19:42
7F:推 Ryspon: void * 算是一个通用型态的概念,用来传递各种型态的参数 10/11 20:20
8F:→ Ryspon: ,传进去之後要自己转型成需要的 type ,把 arg 转型後 as10/11 20:20
9F:→ Ryspon: sign 给你想要的 type pointer ,就像前几楼回覆的范例那10/11 20:20
10F:→ Ryspon: 样10/11 20:20
谢谢楼上的各位! 我建一个新变数存起来型别转换好後就可以了 感谢万分 ※ 编辑: skyHuan (223.137.198.187), 10/11/2018 23:19:33
11F:推 Paravion: 是作业系统的merge sort作业吗 11/01 19:07
12F:推 q79236: Sky葛格 03/13 02:15







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灯, 水草

请输入看板名称,例如:Boy-Girl站内搜寻

TOP