I forgot to note that both synchronized {} blocks should also be synchronizing on the same mutex. Right now it's two different critical sections, so a race is still possible, i.e. while one thread is printing the other may be removing the first printer. Run the code several times and you'll no doubt stumble upon it.

The mutex could be emulated with a shared bool and std.atomic.cas(). That would get rid of synchronized{} blocks and would potentionally be faster.

Reply via email to