views:

56

answers:

0

Hi,

I have deadlock detected error in my code and don't understand why.

Could someone please tell me what am I doing wrong?

#!/usr/bin/ruby
ENV['RAILS_ENV'] = ARGV.first || ENV['RAILS_ENV'] || 'development'  
require File.expand_path(File.dirname(__FILE__) + "/config/environment")

mutex = Mutex.new
threads = []
1.upto(10) do |i|
  threads << Thread.new(i) do |id|
    mutex.synchronize do
      # here I want to take 1 record from "class Product < ActiveRecord::Base"
      Product.first
    end
    # and to do here some stuff with it
    # ...
    # but all I get is
    # ./sandbox.rb:15:in `join': deadlock detected (fatal)
  end
end
threads.each { |thread| thread.join }

I use rails 3 and ruby 1.9.2