Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Threads and external methods

Reply
Thread Tools

Threads and external methods

 
 
Wonderdog
Guest
Posts: n/a
 
      02-07-2007
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

 
Reply With Quote
 
 
 
 
Pit Capitain
Guest
Posts: n/a
 
      02-08-2007
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

 
Reply With Quote
 
 
 
 
Pit Capitain
Guest
Posts: n/a
 
      02-08-2007
Pit Capitain 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a way to find the class methods of a class, just like'methods' finds the instance methods? Kenneth McDonald Ruby 5 09-26-2008 03:09 PM
Threads: wait and resume methods. Ben Java 4 07-26-2006 03:14 PM
[new to threads] threads with UI and loop Une bévue Ruby 0 06-14-2006 10:22 AM
About Threads and Shared Methods ??? Gilles Lambert ASP .Net 1 04-26-2004 02:59 PM
Re: Templates and friends and template methods with private methods. Buster Copley C++ 5 07-07-2003 12:50 AM



Advertisments