Created
November 18, 2014 10:04
-
-
Save anonymous/53262943877ddb7a026a 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
#!/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