# Threads and external methods

**URL:** <https://www.ruby-forum.com/t/threads-and-external-methods/84693>\
**Category:** Ruby\
**Created:** [February 7, 2007, 1:50am UTC](https://www.ruby-forum.com/t/threads-and-external-methods/84693 "2007-02-07T01:50:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![wonderdog](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ecd19e/32.png) [@wonderdog](https://www.ruby-forum.com/u/wonderdog)\
**Post date:** [February 7, 2007, 1:50am UTC](https://www.ruby-forum.com/t/threads-and-external-methods/84693/1 "2007-02-07T01:50:35Z")

</div>

If I have require’d a module and call a method within that module from  
within a thread, do all the variables within that external module need  
to be made thread safe?

test.rb ==\>  
require ‘mod’  
threads = []  
(1…20).each do  
threads \<\< Thread.new( var ) { |var|

# call a function/method within mod

modFunction( var )  
}  
threads.each {|a| a.join}

mod.rb ==\>  
def modFunction( myvar )

# do lotsa copmlex stuff with myvar

…  
end

===

Does the ‘myvar’ variable above need to be ‘Thread.current[“myvar”]’  
instead?

Thanks for helping this noob out!  
wd

---

<div class="post-metadata">

**Author:** ![wonderdog](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ecd19e/32.png) [@wonderdog](https://www.ruby-forum.com/u/wonderdog)\
**Post date:** [February 8, 2007, 3:19pm UTC](https://www.ruby-forum.com/t/threads-and-external-methods/84693/2 "2007-02-08T15:19:23Z")

</div>

Hi wd!

> If I have require’d a module …

BTW: in Ruby you require a file, which might contain a module  
definition.

> …and call a method within that module from  
> within a thread, do all the variables within that external module need  
> to be made thread safe?

In this context, it doesn’t matter whether the code you execute comes  
from an “external” module or not.

> test.rb ==\>  
> require ‘mod’  
> threads = []  
> (1…20).each do  
> threads \<\< Thread.new( var ) { |var|
> 
> # call a function/method within mod
> 
> modFunction( var )  
> }  
> threads.each {|a| a.join}

Where does the variable “var” which you pass to Thread.new come from?  
What value does it have? Are you deliberately using the name “var” as  
the name of both a local variable and a block parameter?

> mod.rb ==\>  
> def modFunction( myvar )
> 
> # do lotsa copmlex stuff with myvar
> 
> …  
> end
> 
> ===
> 
> Does the ‘myvar’ variable above need to be ‘Thread.current[“myvar”]’  
> instead?

I don’t think so. Maybe you can show us a short working example of your  
code and tell us what didn’t work as you expected.

Regards,  
Pit

---

<div class="post-metadata">

**Author:** ![wonderdog](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ecd19e/32.png) [@wonderdog](https://www.ruby-forum.com/u/wonderdog)\
**Post date:** [February 8, 2007, 3:45pm UTC](https://www.ruby-forum.com/t/threads-and-external-methods/84693/3 "2007-02-08T15:45:25Z")

</div>

Pit C. schrieb:

> (…) Maybe you can show us a short working example of your  
> code and tell us what didn’t work as you expected.

Sorry, what I meant was a short example code that can be executed…

Regards,  
Pit
