我的作業是執行等待和信號在一個菱形圖案的信號量數組。信號量作爲鑽石
下面是模式:
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