背景:
寫了個程式,主要實作的功能為主線程建立兩個子線程,之後主線程進入等待事件的循環。子線程1對臨界資源cnt的值減2并輸出cnt的值。子線程2對cnt的值加1并輸出cnt的值,且當cnt為50的時候觸發事件對象hend_even,子程序2退出。主線程收到hend_even後殺死子線程1,之後進入scanf阻塞。。。
代碼示例:
運作結果:
thread a delete 2 of cnt = 98
thread b add 1 of cnt = 99
thread a delete 2 of cnt = 97
thread b add 1 of cnt = 98
thread a delete 2 of cnt = 96
thread b add 1 of cnt = 97
thread a delete 2 of cnt = 95
thread b add 1 of cnt = 96
thread a delete 2 of cnt = 94
thread b add 1 of cnt = 95
thread a delete 2 of cnt = 93
thread b add 1 of cnt = 94
thread a delete 2 of cnt = 92
thread b add 1 of cnt = 93
thread a delete 2 of cnt = 91
thread b add 1 of cnt = 92
thread a delete 2 of cnt = 90
thread b add 1 of cnt = 91
thread a delete 2 of cnt = 89
thread b add 1 of cnt = 90
thread a delete 2 of cnt = 88
thread b add 1 of cnt = 89
...
thread a delete 2 of cnt = 51
thread b add 1 of cnt = 52
thread a delete 2 of cnt = 50
thread b add 1 of cnt = 51
thread a delete 2 of cnt = 49
thread b add 1 of cnt = 50
proc b stop to sail ticket...
back to main thread...
參考:
http://www.cnblogs.com/1314nnnkkk/p/3386375.html
http://hi.baidu.com/kdlipm/item/005be8eb29d785e1fa42bae5