Mutex object

Discussion in 'Ruby' started by ash 34, Mar 7, 2010.

  1. ash 34

    ash 34 Guest

    Should I create a new Mutex object per thread accessing a shared
    resource or is it per shared resource being accessed. I understand that
    I need to create a Mutex object and use the synchronize method to make
    the associated block appear as an atomic operation but not sure how to
    decide when and how many Mutex objects need to be created. Can someone
    explain.

    thanks.
    --
    Posted via http://www.ruby-forum.com/.
    ash 34, Mar 7, 2010
    #1
    1. Advertising

  2. ash 34

    p0Eta Guest

    You need one mutex by resource. The Mutex will control the access to
    that object, giving exclusive permission for a thread to modify it.
    If you have a list and a hash, for example, that will be shared by
    three threads, you'll need two mutexes: one for the list, and one for
    the hash. While a thread modifies the list, others can modify the
    hash, and so on. It's a good practice to always synchronise multiple
    mutexes in the same order, to prevent deadlocks.
    p0Eta, Mar 8, 2010
    #2
    1. Advertising

  3. 2010/3/8 p0Eta <>:
    > You need one mutex by resource. The Mutex will control the access to
    > that object, giving exclusive permission for a thread to modify it.
    > If you have a list and a hash, for example, that will be shared by
    > three threads, you'll need two mutexes: one for the list, and one for
    > the hash.


    This is only true if the Array and the Hash are supposed to be
    manipulated independently. If they need to be changed in sync, you
    need a single mutex.

    > While a thread modifies the list, others can modify the
    > hash, and so on. It's a good practice to always synchronise multiple
    > mutexes in the same order, to prevent deadlocks.


    ... if you need to hold more than one lock, yes.

    OP, you need one mutex or monitor per critical section. How many
    objects you manipulate in that critical section is totally dependent
    on the logic to execute. If you allocate mutexes per thread, you do
    not have mutexes ("mutual exclusion") - assuming each thread gets its
    own mutex.

    Kind regards

    robert

    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
    Robert Klemme, Mar 8, 2010
    #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. William LaMartin

    What permissions for Mutex to be shared?

    William LaMartin, Oct 16, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    352
    William LaMartin
    Oct 16, 2003
  2. Raghuvansh

    Mutex could not be created.

    Raghuvansh, May 18, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    567
    Raghuvansh
    May 18, 2004
  3. NaeiKinDus
    Replies:
    1
    Views:
    566
    Jack Klein
    Apr 14, 2007
  4. NaeiKinDus
    Replies:
    3
    Views:
    600
    James Kanze
    Apr 15, 2007
  5. sven
    Replies:
    2
    Views:
    1,929
    Roy Smith
    Dec 4, 2009
Loading...

Share This Page