Last active
February 2, 2025 06:30
-
-
Save tompng/12a8af11fdc1dc4b972fc299c2a8bf02 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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