Skip to content

Instantly share code, notes, and snippets.

@sreeix
Created November 28, 2011 16:54
Show Gist options
  • Select an option

  • Save sreeix/1401069 to your computer and use it in GitHub Desktop.

Select an option

Save sreeix/1401069 to your computer and use it in GitHub Desktop.
Threads are hard
m = Mutex.new
t = []
1.upto(50) do |i|
t << Thread.new(i) do |id|
m.synchronize do
puts i
User.first
end
end
end
t.each { |th| th.join }
$ rails/runner foo.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
script/foo:11:in `join': deadlock detected (fatal)
from script/foo:11:in `block in <top (required)>'
from script/foo:11:in `each'
from script/foo:11:in `<top (required)>'
from /Users/sreeix/.rvm/gems/ruby-1.9.2-p180@spaghetti/gems/rails-2.3.11/lib/commands/runner.rb:46:in `eval'
from /Users/sreeix/.rvm/gems/ruby-1.9.2-p180@spaghetti/gems/rails-2.3.11/lib/commands/runner.rb:46:in `<top (required)>'
from script/runner:3:in `require'
from script/runner:3:in `<main>'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment