Skip to content

Instantly share code, notes, and snippets.

@DiegoSalazar
Created February 12, 2014 18:43
Show Gist options
  • Save DiegoSalazar/8961861 to your computer and use it in GitHub Desktop.
Save DiegoSalazar/8961861 to your computer and use it in GitHub Desktop.
test variables are shared between threads
# testing out class variables are shared between instances
require 'thread'
class A
@m = Mutex.new
@@m = Mutex.new
M = Mutex.new
def self.m; @m end
def self.mm; @@m end
def self.mmm; M end
end
puts "nil #{nil.object_id}"
@output = {}
def gather_output
name = "Thread #{Thread.current.object_id}"
@output[name] = []
@output[name] << "c #{A}"
@output[name] << "@m #{A.m}"
@output[name] << "@@m #{A.mm}"
@output[name] << "M #{A.mmm}"
end
threads = []
3.times do
threads << Thread.new(&method(:gather_output))
end
gather_output
threads.map &:join
@output.each_pair do |name, o|
puts [name, o].inspect
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment