# Thread-local global variables?

**URL:** <https://www.ruby-forum.com/t/thread-local-global-variables/143689>\
**Category:** Ruby\
**Created:** [July 21, 2008, 9:18pm UTC](https://www.ruby-forum.com/t/thread-local-global-variables/143689 "2008-07-21T21:18:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![eclubb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ecc23a/32.png) [@eclubb](https://www.ruby-forum.com/u/eclubb)\
**Post date:** [July 21, 2008, 9:18pm UTC](https://www.ruby-forum.com/t/thread-local-global-variables/143689/1 "2008-07-21T21:18:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![eclubb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ecc23a/32.png) [@eclubb](https://www.ruby-forum.com/u/eclubb)\
**Post date:** [July 21, 2008, 9:29pm UTC](https://www.ruby-forum.com/t/thread-local-global-variables/143689/2 "2008-07-21T21:29:32Z")

</div>

Earle C. wrote:

> 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.)

---

<div class="post-metadata">

**Author:** ![eclubb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ecc23a/32.png) [@eclubb](https://www.ruby-forum.com/u/eclubb)\
**Post date:** [July 21, 2008, 9:37pm UTC](https://www.ruby-forum.com/t/thread-local-global-variables/143689/3 "2008-07-21T21:37:10Z")

</div>

On Jul 21, 2008, at 1:23 PM, Joel VanderWerf wrote:

> 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/](http://codeforpeople.com/)

---

<div class="post-metadata">

**Author:** ![eclubb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ecc23a/32.png) [@eclubb](https://www.ruby-forum.com/u/eclubb)\
**Post date:** [July 21, 2008, 10:00pm UTC](https://www.ruby-forum.com/t/thread-local-global-variables/143689/4 "2008-07-21T22:00:42Z")

</div>

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.)
