Threads and external methods

Discussion in 'Ruby' started by Wonderdog, Feb 7, 2007.

  1. Wonderdog

    Wonderdog Guest

    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
     
    Wonderdog, Feb 7, 2007
    #1
    1. Advertising

  2. Wonderdog

    Pit Capitain Guest

    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
     
    Pit Capitain, Feb 8, 2007
    #2
    1. Advertising

  3. Wonderdog

    Pit Capitain Guest

    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
     
    Pit Capitain, Feb 8, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Scott Allen
    Replies:
    8
    Views:
    11,021
    Scott Allen
    May 2, 2004
  2. Une bévue
    Replies:
    0
    Views:
    178
    Une bévue
    Jun 14, 2006
  3. Kenneth McDonald
    Replies:
    5
    Views:
    356
    Kenneth McDonald
    Sep 26, 2008
  4. Mellow Crow
    Replies:
    6
    Views:
    431
    Richard Cornford
    Nov 4, 2005
  5. Joug Raw
    Replies:
    1
    Views:
    121
    Chris Angelico
    Aug 15, 2013
Loading...

Share This Page