I'm trying to create a Stats counter (similar to the one in ostrich for scala by Twitter) but having a hard time making sure that all my threads have access to this. My Stats class is defined like this:
class Stats
@@counters = {}
.. accessors ..
def self.incr(counter, amt = 1)
if !@@counters[counter]
@@counters[counter] = java.util.concurrent.atomic.AtomicInteger.new()
end
@@counters[counter].getAndAdd(amt)
end
end
I know there are some issues with the thread-safety of the counters hash itself. If I create threads manually, they seem to be able to access the Stats.counters globally but I'm trying to create a rackup application (Sinatra, embedded in Jetty using jetty-rackup) to show this info, and in that Sinatra application this Stats is empty. Is there a good way to share this counter with other parts of the application or is sinatra doing something that's clearing out the global variable scope?