Thread-local global variables?

E

Earle Clubb

Is it possible to define a variable whose value is global within a
particular each thread (including the main thread), yet distinct between
threads? For example, I've tried this:

$abc = 0

threads = []
2.times do |i|
threads << Thread.new(i+1) do |i|
$abc = i
10.times do
print "thread #{i}: abc = #{$abc}\n"
sleep 0.5
end
end
end

10.times do
print "thread 0: abc = #{$abc}\n"
sleep 0.5
end

threads.each {|t| t.join}


Of course it prints:

thread 1: abc = 1
thread 2: abc = 2
thread 0: abc = 2
thread 1: abc = 2
thread 0: abc = 2
thread 2: abc = 2
thread 0: abc = 2
thread 2: abc = 2
thread 1: abc = 2
...

Is there any way to do this so that it prints like this?

thread 1: abc = 1
thread 2: abc = 2
thread 0: abc = 0
thread 1: abc = 1
thread 0: abc = 0
thread 2: abc = 2
thread 0: abc = 0
thread 2: abc = 2
thread 1: abc = 1
...

Thanks,
Earle
 
J

Joel VanderWerf

Earle said:
Is it possible to define a variable whose value is global within a
particular each thread (including the main thread), yet distinct between
threads? For example, I've tried this:

the closest approximation is thread-locals:

thread = Thread.current
thread[:my_var] = "value"

(I've often thought it would be nice to have a syntax for per-thread
globals, like $_x or something.)
 
A

ara.t.howard

he closest approximation is thread-locals:

thread = Thread.current
thread[:my_var] = "value"

(I've often thought it would be nice to have a syntax for per-thread
globals, like $_x or something.)



class Module
def thattr name
module_eval <<-code
def #{ name }() Thread.current[#{ name.to_s.inspect }] end
def #{ name }=(v) Thread.current[#{ name.to_s.inspect }] = v end
code
end
end

or similar is what i use

a @ http://codeforpeople.com/
 
J

Joel VanderWerf

class Module
def thattr name
module_eval <<-code
def #{ name }() Thread.current[#{ name.to_s.inspect }] end
def #{ name }=(v) Thread.current[#{ name.to_s.inspect }] = v end
code
end
end

class Foo
thattr :x
end

foo = Foo.new
bar = Foo.new
foo.x = 1
p bar.x # ==> 1

This looks a little weird to me. But it does have the advantage that the
method is defined only in the scope where you use it. (In a way, that's
a disadvantage, too, since it obscures the fact that "x" really does
have a bigger scope, namely the Thread.current#[] method.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top