title: Ruby threads and mutex
body: <p>Why does the following ruby code not work?</p>
  
  <pre><code> 2 | require 'thread'
   3 | 
   4 | $mutex = Mutex.new
   5 | $mutex.lock
   6 |
   7 | t = Thread.new {
   8 |   sleep 10
   9 |   $mutex.unlock
  10 | }
  11 | 
  12 | $mutex.lock
  13 | puts "Delayed hello"
  </code></pre>
  
  <p>When I'm running it, I get an error:</p>
  
  <pre><code>./test.rb:13:in `lock': thread 0x7f4557856378 tried to join itself (ThreadError)
      from ./test.rb:13
  </code></pre>
  
  <p>What is the right way to synchronize two threads without joining them (both threads must continue running after synchronization)?</p>