s**9 发帖数: 207 | 1 问题:一个writer线程向一个queue写数据,另一个reader线程从中读数据.读的时候如果
为空, reader应当block.
Solution:
In the main thread, initialize a semapore S to 0, initialize a mutex M.
In the write thread, for each write, lock M, write, unlock M, post S.
In the reader thread, for each read: wait S, lock M, read, unlock M.
上面的解法有没有问题? 其它有更好的算法么? | n**x 发帖数: 30 | 2 是不是写满了就会出问题?如果一直是write的话会满的,但程序里会溢出。
可以这样改:
main thread: S=0,E=N,init M
write thread: wait(E), lock(M), write,unlock(M),post(S)
read thread: wait(S), lock(M), read, unlock(M),pose(E)
ref:Operating System Concepts(7th) 6.6.1 |
|