新しい条件変数 non_full
と size
フィールドを追加します。
type 'a t =
{ queue : 'a Queue.t; size : int; lock : Mutex.t;
non_empty : Condition.t; non_full : Condition.t; }
let create k =
if k > 0 then
{ queue = Queue.create (); size = k; lock = Mutex.create ();
non_empty = Condition.create (); non_full = Condition.create () }
else failwith "Tqueue.create: empty size";;
要素の追加は add
と take
の処理を組み合わせたものになります。
let add x q =
Mutex.lock q.lock;
while Queue.length q.queue = q.size
do Condition.wait q.non_full q.lock done;
if Queue.is_empty q.queue then Condition.broadcast q.non_empty;
Queue.add q x;
Mutex.unlock q.lock;;
要素の削除は追加と似ていますが、削除する前のキューが満杯な場合には non_full
をシグナルする必要があります。
size
を max_int
とすれば動作が制限のないキューと同じになります。
* * *