-
-
Save dminuoso/5efaebc1f85547f4971711b92aeef5fd to your computer and use it in GitHub Desktop.
This file contains 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
class TQueue | |
def initialize | |
@r = Concurrent::TVar.new [] | |
@w = Concurrent::TVar.new [] | |
end | |
def write(a) | |
t = Concurrent::Transaction.current | |
as = t.read(@w) | |
as.push(a) | |
t.write(@w, as) | |
end | |
def read | |
t = Concurrent::Transaction.current | |
r = t.read(@r) | |
if !r.empty? | |
x, *xs = r | |
t.write(@r, xs) | |
return x | |
else | |
ys = t.read(@w) | |
if ys.empty? | |
Concurrent.abort_transaction | |
else | |
z, *zs = ys | |
t.write(@w, []) | |
t.write(@r, zs) | |
return z | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment