buildingenter() s1.wait NR.wait D.wait d1.lock s1.release D.release D.wait d2.lock d1.unlock D.release D.wait d3.lock d2.unlock D.release D.wait d3.unlock puts 'entered building' NR.release buildingleave() s2.wait ... puts 'left building' rotate() while NR.wait if(d1/d2/d3/d4) rotate90 NR.release i'm trying to implement a rotating door of a building (which is divided to 4 parts, each could contain only 1 thread), with two queues of 'threads', for entry and exit, which are going in and out of the building. 'rotate' is a function which rotates the door by 90 degrees (and hence forwarding the lines by 1) and rotate shouldnt work until threads have exited or entered the door. I'm stuck with getting them to switch turns without getting a deadlock