A
Adam Bender
I want to modify the Queue class in thread.rb, so I first searched for
the file. I found it in /usr/lib/ruby/1.8/thread.rb, both on a Mac
and a Linux machine. On the Mac, the file was as expected, but on the
Linux machine, it contained only this:
-----------------------
unless defined? Thread
fail "Thread not available for this ruby interpreter"
end
require 'thread.so'
-----------------------
Does this mean the Ruby code for Queue is compiled into a shared
library? Is it possible to edit the code? (I know I can modify the
class from outside the file, and that is probably what I will end up
doing, but I'm rather surprised by the above file contents and would
like to know more about it).
Thanks,
Adam
P.S. On a related note, I want to add a method to the Queue class that
is similar to pop, but will take a timeout value and return nil if it
no item is push'ed before timeout seconds have elapsed. Can anyone
suggest something better than the following:
class Queue
def pop_with_timeout(timeout)
elapsed = 0.0
while (Thread.critical = true; @que.empty?)
@waiting.push Thread.current
Thread.critical = false
elapsed += sleep(timeout - elapsed)
puts elapsed
return nil if elapsed >= timeout
end
@que.shift
ensure
Thread.critical = false
end
end
That was the first thing I came up with, and I'm (again) surprised
that it works - why does sleep ever return less than 1? What
interrupts it?
the file. I found it in /usr/lib/ruby/1.8/thread.rb, both on a Mac
and a Linux machine. On the Mac, the file was as expected, but on the
Linux machine, it contained only this:
-----------------------
unless defined? Thread
fail "Thread not available for this ruby interpreter"
end
require 'thread.so'
-----------------------
Does this mean the Ruby code for Queue is compiled into a shared
library? Is it possible to edit the code? (I know I can modify the
class from outside the file, and that is probably what I will end up
doing, but I'm rather surprised by the above file contents and would
like to know more about it).
Thanks,
Adam
P.S. On a related note, I want to add a method to the Queue class that
is similar to pop, but will take a timeout value and return nil if it
no item is push'ed before timeout seconds have elapsed. Can anyone
suggest something better than the following:
class Queue
def pop_with_timeout(timeout)
elapsed = 0.0
while (Thread.critical = true; @que.empty?)
@waiting.push Thread.current
Thread.critical = false
elapsed += sleep(timeout - elapsed)
puts elapsed
return nil if elapsed >= timeout
end
@que.shift
ensure
Thread.critical = false
end
end
That was the first thing I came up with, and I'm (again) surprised
that it works - why does sleep ever return less than 1? What
interrupts it?