Skip to content

Instantly share code, notes, and snippets.

Created November 18, 2014 10:04
Show Gist options
  • Save anonymous/53262943877ddb7a026a to your computer and use it in GitHub Desktop.
Save anonymous/53262943877ddb7a026a to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'socket'
require 'thread'
def foo
queue = Queue.new
semaphore = Mutex.new
threads = 16
(1000).times {|i| queue << i } # obviously this isn't going to work :(
threads.times.map {
until queue.empty?
Thread.new do
begin
addr = [queue.pop].pack('V').unpack('C4')
name = Socket.gethostbyaddr(addr.pack('C4')).first
semaphore.synchronize {
yield addr.join('.'), name if block_given?
}
rescue SocketError => serr
semaphore.synchronize {
yield addr.join('.'), serr.message if block_given?
}
end
end
end
}.each(&:join)
end
foo { |addr, name| puts "#{addr} ---> #{name}" }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment