title: Ruby: How do I share a global variable amongst threads that are running an object.method
body: <p>I am running a ruby script that creates several threads. </p>
  
  <p>I want to make the threads have access to a common variable that lets the main thread know when to join the threads.</p>
  
  <p>I am trying to do this with a $global variable, but the threads don't seem the be able to access the $global.</p>
  
  <pre><code>class IO_ 
    def change(number)
      sleep(60 * number)
      $trade_executed = true
    end
  end 
  
  io = IO_.new 
  numbers = 1, 2
  $threads = {}
  $trade_executed = false
  
  def start_threads(numbers)
    numbers.each do |number|
      $threads[number] = Thread.new {io.change(number)}
    end
  end
  
  start_threads(numbers)
  
  while true
    p $trade_executed
    p $threads
    sleep(10)
  end
  </code></pre>
  
  <p>The above $trade_executed will always be 'false'. </p>
  
  <p>But if I move the method change outside of the io object it works.</p>