Skip to content

@DiegoSalazar /var_thread_test.rb
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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
Something went wrong with that request. Please try again.