Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Index: lib/thread.rb
===================================================================
--- lib/thread.rb (リビジョン 28689)
+++ lib/thread.rb (作業コピー)
@@ -148,6 +148,7 @@
@waiting.taint
self.taint
@mutex = Mutex.new
+ @relocked = ConditionVariable.new
end
#
@@ -158,7 +159,10 @@
@que.push obj
begin
t = @waiting.shift
- t.wakeup if t
+ if t
+ t.wakeup
+ @relocked.wait(@mutex)
+ end
rescue ThreadError
retry
end
@@ -187,6 +191,7 @@
raise ThreadError, "queue empty" if non_block
@waiting.push Thread.current
@mutex.sleep
+ @relocked.signal
else
return @que.shift
end
# Test for Queue fairness
# just testing - the fairness is not a spec at present.
create_waiter = lambda { |name, command|
t = Thread.new {
p [command.pop, name]
}
Thread.pass until t.status == 'sleep'
t
}
50.times do
command = Queue.new
# create 3 waiters, and...
create_waiter.call(:first, command)
create_waiter.call(:second, command)
create_waiter.call(:third, command)
# and pushes a command.
command.push(:go)
# then try to consume the command by a subsequent thread
Thread.new {
p [command.pop, :LATER]
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment