Skip to content

Instantly share code, notes, and snippets.

@tompng
Last active February 2, 2025 06:30
Show Gist options
  • Save tompng/12a8af11fdc1dc4b972fc299c2a8bf02 to your computer and use it in GitHub Desktop.
Save tompng/12a8af11fdc1dc4b972fc299c2a8bf02 to your computer and use it in GitHub Desktop.
mut = Ractor.new do
pipe = Ractor.new do
loop { Ractor.yield Ractor.receive }
end
loop do
Ractor.yield pipe
pipe.take
end
end
def mut.synchronize
r = take
yield
ensure
r.send :synchronize_end
end
ractors = 100.times.map do |n|
Ractor.new(mut,n) do |mut,n|
2.times do
mut.synchronize do
p n => :start
sleep 0.01
p n => :end
end
end
end
end
ractors.map(&:take)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment