Skip to content

Instantly share code, notes, and snippets.

@dminuoso
Created June 8, 2018 08:13
Show Gist options
  • Save dminuoso/5efaebc1f85547f4971711b92aeef5fd to your computer and use it in GitHub Desktop.
Save dminuoso/5efaebc1f85547f4971711b92aeef5fd to your computer and use it in GitHub Desktop.
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