Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save NZKoz/31565 to your computer and use it in GitHub Desktop.
Save NZKoz/31565 to your computer and use it in GitHub Desktop.
require 'thread'
class BuggeredLogger
def initialize
@buffer = []
@mutex = Mutex.new
end
def maybe_flush
flush if @buffer.size > 10
end
def flush
return unless @buffer.size > 0
@mutex.synchronize do
# better check again, there's a race condition
return unless @buffer.size > 0
puts "*** #{@buffer.slice!(0..-1).size}"
end
end
def log(msg)
@mutex.synchronize do
@buffer << msg
end
maybe_flush
end
end
$logger = BuggeredLogger.new
threads = (1..50).map do |i|
Thread.new do
loop do
$logger.log("HEY MAN, I AM #{i}")
puts "logged from #{i}"
end
end
end
# run the hax for 60 seconds
sleep(30)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment