2012年2月21日星期二

LOCK CV

OS161 PLATFORM

LOCK IMPLEMENTATION


CV(condition variable) is for multi thread use. wait-wakeup
eg: T1, T2, T1 need test_cond(bool type) is true to be run. And test_cond is controlled by T2. In this case, how to achieve this?

Method 1:

T1 is sleeping and wake up every 5ns (arbitrary, can be any time period) come to examine the value of test_cond,if it is false, keep sleep; otherwise, start to run.

Method2:

T1 call cond_wait and wait when test_cond is false, when the value of test_cond is changed, T2 will call cond_signal to wake up T1(which is sleep & busy waitng), tell T1: hi, the value of test_cond is changed. T1: got you, I am going to run.

We can see the scenario of the bathroom on a plane, T1(old lady)and T2(old man) are two people, T2 is in the bathroom, T1 want to use it...So T1 stand up again and again to see if the bathroom is available or not.(if there is no light to indicate that). That is a waste, it waste T1's energy , it influence the beautiful waitress to look after her, it waste other customers' energy to see her around.

So here we go,.we have a light outside the bathroom, when T2 is in the bathroom, the light is red, the lady can go back to the seat and sleep, when T2 is done with his work, he will turn the light to green, and the light will call old lady(advanced light), and old lady can use it now.

here is the chinese version.



here and here is the comparison between semaphore, mutex and cv





---------------------------------------------------


-------------------------------------


---------------------------------------------------




没有评论: