我的作业是执行等待和信号在一个菱形图案的信号量数组。信号量作为钻石
下面是模式:
08
06 07
03 04 05
01 02
00
Thread #08 cannot join the diamond until threads #06 and #07 are both in position.
Thread #07 cannot join until threads #04 and #05 are both in position.
Thread #06 cannot join until threads #03 and #04 are both in position. and so on...
到目前为止,我的思想已经取得了以下丑陋的算法:
if (me==0) {
wait_sem (tCount[1]) ;
wait_sem (tCount[2]) ;
signal_sem (tCount[0]) ;
}
if (me==1) {
wait_sem (tCount[3]) ;
wait_sem (tCount[4]) ;
}
if (me==2) {
wait_sem (tCount[4]) ;
wait_sem (tCount[5]) ;
}
if (me==3) {
wait_sem (tCount[6]) ;
}
if (me==4) {
wait_sem (tCount[6]) ;
wait_sem (tCount[7]) ;
}
if (me==5) {
wait_sem (tCount[7]) ;
}
if (me==6) {
wait_sem (tCount[8]) ;
}
if (me==7) {
wait_sem (tCount[8]) ;
}
是否有这样做的清洁方法?我听说switch
,但我从来没有使用过,所以如果有人建议,请给我一个解释和例子。非常感谢所有的投入。
快速的问题,我会将这个代码包含在涉及算法的函数中,还是我应该把它放到全局访问? – Junikin
很难快速回答这个问题。 – paddy
好的,我明白了。那么我会玩一下。非常感谢 – Junikin