Created
February 12, 2014 18:43
-
-
Save DiegoSalazar/8961861 to your computer and use it in GitHub Desktop.
test variables are shared between threads
This file contains 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
# 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