C_and_CPP 板


LINE

source: an embedded software primer 1ed by Simon 正确的code: https://ideone.com/pRhTMU 错误的code: https://ideone.com/M2ZRJB 很明显地可以看出这两段程式码的差别在於: 一个有用函式lSecondsSinceMidnight把变数 lSecondsToday包起来,一个是直接对变数lSecondsToday做access的动作。 这两段code都有用semaphore把lSecondsToday给包起来。 作者说第二种错误的方式会有bug,但,既然都已经用semaphore把lSecondsToday包起来了 ,怎麽会有bug的发生呢?为什麽要多用一层lSecondsSinceMidnight把变数给包起来? 打到这里突然看到作者说的一段话: At least some of bugs stem from undisciplined use: allowing code in many diffe- rent modules to use the same semaphore and hoping that they all use it correct- ly. You can squash there bugs before they get crawling simply by hiding the semaph- ore and the data that is protects inside of a module, thereby encapsulating bo- th. 作者说把semaphore与semaphore要保护的data放在module里可以防止"code in many diff- erent modules to use the same semaphore". 但...在第一种正确的方式,function vHackerTask与vJuniorProgrammerTask都呼叫同一 个function lSecondsSinceMidnight,不也是"use the same semaphore"吗? 想请教各位前辈是不是我误会了什麽?谢谢 --



※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 61.216.85.243
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/C_and_CPP/M.1530244718.A.D72.html
1F:→ bluesoul: 第二段code回圈的部分没有保护 06/29 12:06
有,回圈上有comment,有说hope junior remembers to use the semaphore
2F:→ sarafciel: 他那段话不是在否定use the same of semaphore这件事 06/29 13:20
3F:→ sarafciel: 简单的讲就是 你不知道用你的code的人是雷包还是大师 06/29 13:23
4F:→ sarafciel: 那麽让他们去写semaphore 你也不知道他们写得对不对 06/29 13:26
5F:→ sarafciel: 还不如你自己实作正确的同步函式给他们call比较好 06/29 13:26
6F:→ sarafciel: 讲同步不太精确....应该说可平行化 06/29 13:28
其实我认为这段不是在表达这个意思。GetSemaphore与GiveSemaphore都是厂商提供的fun- ction,作者是站在"function的使用者"的角度去表达这段话 ※ 编辑: zzss2003 (60.251.49.183), 06/29/2018 17:59:39
7F:→ sarafciel: "hope junior remembers" 你怎麽会理解成他有保护.... 06/30 13:51
8F:→ sarafciel: 他里面那三个.c档 是不同的人在写的code 06/30 13:55
9F:→ sarafciel: 第一个vtimertask()只是跟你示意说lSecondsToday需要被 06/30 13:59
10F:→ sarafciel: 保护 所以後面hacker跟junior这两个想使用它的时候 06/30 14:01
11F:→ sarafciel: 应该也要用semphore保护 问题是他不知道hacker跟junor 06/30 14:02
12F:→ sarafciel: 他们会不会加 或着是写一写忘记这个变数是该被保护的结 06/30 14:04
13F:→ sarafciel: 果没加 那最保险的做法就是他们要用的时候封装成一个 06/30 14:04
14F:→ sarafciel: function去保护变数 06/30 14:08
15F:→ sarafciel: 至於use the same semaphore 我换个角度跟你讲 06/30 14:11
16F:→ sarafciel: 我把变数包起来 跟把整个回圈 甚至整个main包起来 06/30 14:15
17F:→ sarafciel: 这些也都是use the same semaphore 问题是後面两个 06/30 14:17
18F:→ sarafciel: 会把semaphore拿着太久 造成其他thread不必要的等待 06/30 14:18
谢谢你的解释,我不懂的原因是因为我卡在这句英文: At least some of those bugs stem from undisciplined use: allowing code in many different modules to use the same semaphore and hoping that they all use it correctly. 我一开始把"允许不同module内的code使用一样的semaphore"认为是undisciplined use, 仔细看了一遍,原来作者的意思是"允许不同module内的code使用一样的semaphore,且希 望他们正确地使用"这一整段话才是undisciplined use。 ※ 编辑: zzss2003 (60.251.49.183), 07/02/2018 13:59:34







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

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

TOP