Thread safety

Discussion in 'Ruby' started by Jeff Turc, Feb 15, 2008.

  1. Jeff Turc

    Jeff Turc Guest

    With the discussion surrounding merb/rails and thread safety, I've been
    somewhat concerned with making my own tiny home-rolled web apps that I
    write thread safe, if only to become more knowledgeable on the subject.

    I know it's probably hard to generalize, but does anybody have any
    simple rules to follow (or resources to share) for writing thread safe
    code?

    Thanks!
    --
    Posted via http://www.ruby-forum.com/.
     
    Jeff Turc, Feb 15, 2008
    #1
    1. Advertising

  2. Jeff Turc

    s.ross Guest

    On Feb 15, 2008, at 10:54 AM, Jeff Turc wrote:

    > With the discussion surrounding merb/rails and thread safety, I've
    > been
    > somewhat concerned with making my own tiny home-rolled web apps that I
    > write thread safe, if only to become more knowledgeable on the
    > subject.
    >
    > I know it's probably hard to generalize, but does anybody have any
    > simple rules to follow (or resources to share) for writing thread safe
    > code?
    >
    > Thanks!



    Anyone else jump right in here. These are mine:

    Rule #1: Don't use global variables.
    Rule #2: If you do, when you change them, wrap them in a blocking
    mechanism such as a mutex or critical section so two threads can't try
    at once.
    Rule #3: If you are changing the state of an object that is not thread-
    local, wrap the change in a mutex (etc.)

    So, for example:

    require 'thread'
    my_object_sync = Mutex.new

    Thread.new do
    # just lock on this object
    my_object_sync.synchronize do
    # Update that is not atomic
    end
    end

    Rule #4: Don't be too sure that any operation in Ruby is atomic.
     
    s.ross, Feb 15, 2008
    #2
    1. Advertising

  3. On 15.02.2008 21:08, s.ross wrote:
    > On Feb 15, 2008, at 10:54 AM, Jeff Turc wrote:
    >
    >> I know it's probably hard to generalize, but does anybody have any
    >> simple rules to follow (or resources to share) for writing thread safe
    >> code?


    > Rule #3: If you are changing the state of an object that is not thread-
    > local, wrap the change in a mutex (etc.)


    Corollary: use thread confinement, i.e. design your app in a way that
    most objects are used in a single thread at a time only. Restrict need
    for synchronization to the minimum needed.

    > Rule #4: Don't be too sure that any operation in Ruby is atomic.


    Somehow this reminds me of "Blondie" and "Atomic". :) Those were the
    days...

    Cheers

    robert
     
    Robert Klemme, Feb 15, 2008
    #3
  4. Jeff Turc

    s.ross Guest

    > Somehow this reminds me of "Blondie" and "Atomic". :) Those were
    > the days...


    One way or another :)
     
    s.ross, Feb 15, 2008
    #4
  5. Jeff Turc

    Jeff Turc Guest

    Steve Ross wrote:
    >> Somehow this reminds me of "Blondie" and "Atomic". :) Those were
    >> the days...

    >
    > One way or another :)


    I'm gonna getcha!

    Thanks guys. big help.
    --
    Posted via http://www.ruby-forum.com/.
     
    Jeff Turc, Feb 15, 2008
    #5
  6. Jeff Turc

    Jeff Turc Guest

    Jeff Turc, Feb 17, 2008
    #6
  7. Jeff Turc

    MenTaLguY Guest

    On Mon, 2008-02-18 at 06:55 +0900, Jeff Turc wrote:
    > For anyone else starting out with threads, I also stumbled across this:
    >
    > 8 Simple Rules for Designing Threaded Applications
    > http://www.devx.com/go-parallel/Article/37034


    I think this is a good article and I wouldn't mind recommending it to
    others.

    Thanks Jeff!

    -mental
     
    MenTaLguY, Feb 21, 2008
    #7
    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. Hans

    What is thread safety?

    Hans, Oct 11, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    602
    Sahil Malik
    Oct 12, 2004
  2. George Ter-Saakov

    LiteralControl thread safety.

    George Ter-Saakov, Apr 5, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    351
    Martin Dechev
    Apr 6, 2004
  3. Simon Harvey

    A thread safety question

    Simon Harvey, Aug 6, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    419
    Alvin Bruney [MVP]
    Aug 6, 2004
  4. thechaosengine

    Thread safety when subclassing the Page class

    thechaosengine, Dec 10, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    411
    Scott Allen
    Dec 10, 2004
  5. =?Utf-8?B?RGlmZmlkZW50?=

    Thread-safety and Singleton methods

    =?Utf-8?B?RGlmZmlkZW50?=, Jan 13, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    526
    Karl Seguin
    Jan 13, 2005
Loading...

Share This Page