Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

test variables are shared between threads

View var_thread_test.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
# 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
Something went wrong with that request. Please try again.