# Local variables and threads

**URL:** <https://www.ruby-forum.com/t/local-variables-and-threads/96974>\
**Category:** Ruby\
**Created:** [January 25, 2006, 4:05am UTC](https://www.ruby-forum.com/t/local-variables-and-threads/96974 "2006-01-25T04:05:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![russellf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7bcc69/32.png) [@russellf](https://www.ruby-forum.com/u/russellf)\
**Post date:** [January 25, 2006, 4:05am UTC](https://www.ruby-forum.com/t/local-variables-and-threads/96974/1 "2006-01-25T04:05:52Z")

</div>

Is there a way to ensure that a particular variable really is local to a  
thread block? I have a problem with a threaded program that would be  
explained if one of the local variable was actually global. I’ve  
checked everything I can think of…

I miss my and local from perl which allowed me to explicitly control the  
scope of variables.

Russell.

---

<div class="post-metadata">

**Author:** ![russellf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7bcc69/32.png) [@russellf](https://www.ruby-forum.com/u/russellf)\
**Post date:** [January 25, 2006, 4:11am UTC](https://www.ruby-forum.com/t/local-variables-and-threads/96974/2 "2006-01-25T04:11:52Z")

</div>

Russell F. wrote:

> Is there a way to ensure that a particular variable really is local to a  
> thread block? I have a problem with a threaded program that would be  
> explained if one of the local variable was actually global. I’ve  
> checked everything I can think of…
> 
> I miss my and local from perl which allowed me to explicitly control the  
> scope of variables.

Only variables prefixed with $ are global ($var and so on).  
It is, however, certainly possible to have shared locals  
if you have something like

some\_var

t1 = Thread.new { … }  
t2 = Thread.new { … }

Any variables that you define inside the thread block will  
be local to that thread, though.

> Russell.

E

---

<div class="post-metadata">

**Author:** ![russellf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7bcc69/32.png) [@russellf](https://www.ruby-forum.com/u/russellf)\
**Post date:** [January 25, 2006, 9:29am UTC](https://www.ruby-forum.com/t/local-variables-and-threads/96974/3 "2006-01-25T09:29:06Z")

</div>

Russell F. wrote:

> Is there a way to ensure that a particular variable really is local  
> to a thread block? I have a problem with a threaded program that  
> would be explained if one of the local variable was actually global.  
> I’ve checked everything I can think of…
> 
> I miss my and local from perl which allowed me to explicitly control  
> the scope of variables.

A typical problem with threads can be nicely illustrated with this:

09:21:51 [~]: ruby -e 'th=[]

> for i in 0 … 5  
> th \<\< Thread.new do  
> sleep(rand(5))  
> puts i  
> end  
> end  
> th.each {|t| t.join}  
> ’  
> 2  
> 5  
> 5  
> 5  
> 5  
> 5  
> 09:22:21 [~]:

Problem here is that all threads share the local var ‘i’. Solution:

09:22:21 [~]: ruby -e 'th=[]

> for i in 0 … 5  
> th \<\< Thread.new(i) do |x|  
> sleep(rand(5))  
> puts x  
> end  
> end  
> th.each {|t| t.join}’  
> 0  
> 1  
> 5  
> 4  
> 2  
> 3

I.e. provide value(s) as arguments to Thread.new and receive them as  
block  
parameter(s) with different name(s).

HTH

Kind regards

```
robert
```
