multithreading - Point of having more than one condition variable per lock? -


i learning concurrency. understand purpose locks , condition variables serve, in cases 1 want more 1 condition variable per lock?

a condition variable in fact queue on processes can wait (without consuming resources). because can proceed when specific condition holds (hence name). multiple condition variables may useful when different kind of processes must wait different conditions hold. example so-called producer-consumer problem (http://en.wikipedia.org/wiki/producer%e2%80%93consumer_problem), when 2 kinds of processes share common fifo buffer - producers deposit items in it, consumers extract them. producers have wait until buffer has @ least 1 empty slot deposit item to. similarly, consumers may have wait when buffer empty. 2 kinds of processes signal each other: when producer deposits item, signals consumers buffer not empty; when consumer extracts item, signals producers buffer not full. although not impossible solve problem using 1 condition variable, more natural (and efficient) use two: 1 producers, , 1 consumers.

several other "problems" exist need similar approach, e.g. readers-writers , sleeping barber.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -