My threading concern.

Discussion in 'Ruby' started by Aaron Rustad, Apr 9, 2005.

  1. Aaron  Rustad

    Aaron Rustad Guest

    When two theads enter a method that can change a value of an instance
    variable, does the possiblity exist that one thread's changes will
    clobber the other's?

    The reason stems from the ApplicationController of Rails, when a
    request is received, the request is handled by a method that has access
    to instance variables (session, request, params). Coming from the
    Java/Struts world, this is a concern, and methods should not access
    instance variables.

    Is there a different model of theading in Ruby, or, would I be missing
    something in Rails.

    thanks!
    AR.
    Aaron Rustad, Apr 9, 2005
    #1
    1. Advertising

  2. Aaron  Rustad

    Kirk Haines Guest

    Aaron Rustad wrote:

    > When two theads enter a method that can change a value of an instance
    > variable, does the possiblity exist that one thread's changes will
    > clobber the other's?
    >
    > The reason stems from the ApplicationController of Rails, when a
    > request is received, the request is handled by a method that has access
    > to instance variables (session, request, params). Coming from the
    > Java/Struts world, this is a concern, and methods should not access
    > instance variables.
    >
    > Is there a different model of theading in Ruby, or, would I be missing
    > something in Rails.


    Threading is no different in Ruby than it is in other languages, with regard
    to guarding against concurrent access.

    If there is a section of code that should only be accessed in a serial
    fashion, control that access with a Mutex:

    m = Mutex.new
    m.synchronize do
    @foo.update
    @bar.count += 1
    end

    Ruby provides a rich set of support beyond mutexes for dealing with
    threading issues, should you need them.


    Kirk Haines
    Kirk Haines, Apr 9, 2005
    #2
    1. Advertising

  3. On Saturday 09 April 2005 15:29, Aaron Rustad wrote:

    > The reason stems from the ApplicationController of Rails, when a
    > request is received


    [continuing] a new Controller instance is created, and the related instance
    variables are those of this new instance. A controller instance is not reused
    for multiple requests.

    If you'd like to read the source, see actionpack/lib/action_controller/base.rb
    Look for "def process" -- it a class method of Controller.

    --

    Nicholas Seckar aka. Ulysses
    Nicholas Seckar, Apr 9, 2005
    #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. Kenneth Myhra
    Replies:
    2
    Views:
    403
    Kenneth Myhra
    Feb 16, 2004
  2. David Wilding
    Replies:
    6
    Views:
    401
    Whitecrest
    Apr 3, 2004
  3. Neil Zanella
    Replies:
    49
    Views:
    1,008
    Flash Gordon
    Feb 17, 2004
  4. puzzlecracker
    Replies:
    1
    Views:
    366
    Thomas Hawtin
    Sep 21, 2006
  5. Replies:
    1
    Views:
    376
Loading...

Share This Page