Created
November 28, 2011 16:54
-
-
Save sreeix/1401069 to your computer and use it in GitHub Desktop.
Threads are hard
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ 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