Skip to content

Instantly share code, notes, and snippets.

@nahi
Created July 28, 2010 10:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nahi/493930 to your computer and use it in GitHub Desktop.
Save nahi/493930 to your computer and use it in GitHub Desktop.
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