deadlock.rb
| 1 |
# tested on ruby 1.9.1p243 (2009-07-16 revision 24175)
|
|---|---|
| 2 |
# deadlocks after a few seconds.
|
| 3 |
|
| 4 |
require 'thread'
|
| 5 |
require 'socket'
|
| 6 |
|
| 7 |
threads = [] |
| 8 |
|
| 9 |
s1, s2 = ::Socket.pair( ::Socket::AF_UNIX, ::Socket::SOCK_STREAM, 0 ) |
| 10 |
|
| 11 |
threads << ::Thread.new {
|
| 12 |
while( true ) |
| 13 |
p "Hello World!"
|
| 14 |
end
|
| 15 |
} |
| 16 |
|
| 17 |
threads << ::Thread.new( s1 ) { | socket |
|
| 18 |
while( true ) |
| 19 |
s = ::IO.select( nil, [socket], nil, 0.2 ) |
| 20 |
if( s == nil || s[0] == nil ) |
| 21 |
next
|
| 22 |
end
|
| 23 |
sent = socket.syswrite( "A" * 8192 ) |
| 24 |
p "Sent #{sent} bytes."
|
| 25 |
end
|
| 26 |
} |
| 27 |
|
| 28 |
threads << ::Thread.new( s2 ) { | socket |
|
| 29 |
while( true ) |
| 30 |
s = ::IO.select( [socket], nil, nil, 0.2 ) |
| 31 |
if( s == nil || s[0] == nil ) |
| 32 |
next
|
| 33 |
end
|
| 34 |
data = socket.sysread( 8192 )
|
| 35 |
p "Read #{data.length} bytes."
|
| 36 |
end
|
| 37 |
} |
| 38 |
|
| 39 |
threads.each{ |t| t.join }
|