Well, I think that there are a few gotchas that I fell for
I used this pattern quite allot before
done = 0
[Object1, Object2, Object3].each do |runnable|
spawn do
runnable.run
done += 1
end
end
while done < [Object1, Object2, Object3].size
sleep 1
end
I used this to control and manage multiple Fibers running.
Now, it seems like it should still work, but there are a few issues now.
done
is not MT safe now, the code should now be changed to
lock = Mutex.new
done = 0
[Object1, Object2, Object3].each do |runnable|
spawn do
runnable.run
lock.synchronize do
done += 1
end
end
end
while done < [Object1, Object2, Object3].size
sleep 1
end
Only now using Mutex this is “safer”, why “safer” and not “safe”? because in
while done < [Object1, Object2, Object3].size
sleep 1
end
We can’t be sure there isn’t a Thread out there touching the done
mid test.
even using the lock like
lock.synchronize do
while done < [Object1, Object2, Object3].size
sleep 1
end
end
Won’t help us, because those two are guard different parts of the code, so it’s not protecting the done
it’s protecting the specific action of +=
.
The above was only realized today TBH, and already helped me stabilize the code a bit, but it’s hard to think about all those small nuances :)