Multi-threading and Accessing a Mutex
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Multi-threading and Accessing a Mutex

  1. #1
    Join Date
    Mar 2010
    Posts
    14

    Multi-threading and Accessing a Mutex

    Hello everyone.

    I just wanted to check if I was doing this correctly since I couldn't find anything on Google.

    I'm creating a mutex and then checking and then taking ownership of it per thread and then releasing it.

    But I'm not sure if I was taking ownership correctly.

    Heres an example;

    //Creation
    bool good = false;
    HANDLE goodMutex = CreateMutex(NULL,false,"goodMutex");

    //Access per thread
    WaitForSingleObject(goodMutex , INFINITE);
    OpenMutex(SYNCHRONIZE,false, "goodMutex"); //<------ Not sure about this
    good = true;
    ReleaseMutex(goodMutex );


    Can anyone tell me if I'm doing this right?

  2. #2
    Join Date
    Nov 2003
    Posts
    1,813

    Re: Multi-threading and Accessing a Mutex

    See what you can extract from the manual. Let us know if you have problems.
    http://msdn.microsoft.com/en-us/libr...8VS.85%29.aspx
    http://msdn.microsoft.com/en-us/libr...8VS.85%29.aspx

    gg

  3. #3
    Join Date
    Mar 2010
    Posts
    14

    Re: Multi-threading and Accessing a Mutex

    Quote Originally Posted by Codeplug View Post
    See what you can extract from the manual. Let us know if you have problems.
    http://msdn.microsoft.com/en-us/libr...8VS.85%29.aspx
    http://msdn.microsoft.com/en-us/libr...8VS.85%29.aspx

    gg
    I had already taken a look at the CreateMutex page on MSDN, but the named objects one seems to make sense.

    It would seem that I am accessing it correctly.

    My issue was that Create and Close all work with handles, whereas Open doesn't.

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,361

    Re: Multi-threading and Accessing a Mutex

    If you are only synchronizing between threads in the same process, consider using a Critical Section.

    If a cs won't work (because you need to use the WaitForXXX functions, then realize that you can pass the mutex handle to any thread that needs the mutex. In other words, there's no need to perform OpenMutex inside the thread.

  5. #5
    Join Date
    Mar 2010
    Posts
    14

    Re: Multi-threading and Accessing a Mutex

    Quote Originally Posted by Arjay View Post
    If you are only synchronizing between threads in the same process, consider using a Critical Section.

    If a cs won't work (because you need to use the WaitForXXX functions, then realize that you can pass the mutex handle to any thread that needs the mutex. In other words, there's no need to perform OpenMutex inside the thread.
    I'm spawning concurrent threads thats do the same task on different pieces of data with a shared source.

    For example, I have a 3x3 grid that needs to be worked on.
    I spawn a new thread for each of the nodes in that grid.
    But, they share a common piece of data as well, in this case a deqeue that is in the class.

    Thus, there needs to be opening of the mutex in the thread so that the cocurrent threads don't try to work on the data at the same time.

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,361

    Re: Multi-threading and Accessing a Mutex

    Quote Originally Posted by Shard View Post
    I'm spawning concurrent threads thats do the same task on different pieces of data with a shared source.

    For example, I have a 3x3 grid that needs to be worked on.
    I spawn a new thread for each of the nodes in that grid.
    But, they share a common piece of data as well, in this case a deqeue that is in the class.

    Thus, there needs to be opening of the mutex in the thread so that the cocurrent threads don't try to work on the data at the same time.
    Understand that you may not need to use a mutex and can use critical sections. Like I mentioned, critical sections are ideal for in-process synchronization tasks (and they're faster than the kernel mode mutex).

    You can use a critical section to control access to the dequeue. If each 'grid node' thread only ever operates on a single node (without any node overlap), then you may not need to synchronize thread access to each node.

    Of course, you know what you need to do better than I.

  7. #7
    Join Date
    Mar 2010
    Posts
    14

    Re: Multi-threading and Accessing a Mutex

    Quote Originally Posted by Arjay View Post
    Understand that you may not need to use a mutex and can use critical sections. Like I mentioned, critical sections are ideal for in-process synchronization tasks (and they're faster than the kernel mode mutex).

    You can use a critical section to control access to the dequeue. If each 'grid node' thread only ever operates on a single node (without any node overlap), then you may not need to synchronize thread access to each node.

    Of course, you know what you need to do better than I.
    So I took a look at the MSDN description of a critical section and it looked good except for:
    "A critical section object cannot be moved or copied. The process must also not modify the object, but must treat it as logically opaque. "

    My threads heavily modify the lists/objects that I'm currently using mutex's with.

    Thoughts?

  8. #8
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,361

    Re: Multi-threading and Accessing a Mutex

    Quote Originally Posted by Shard View Post
    So I took a look at the MSDN description of a critical section and it looked good except for:
    "A critical section object cannot be moved or copied. The process must also not modify the object, but must treat it as logically opaque. "

    My threads heavily modify the lists/objects that I'm currently using mutex's with.

    Thoughts?
    Msdn is referring to the actual cs object, not the area of code it is protecting. The critical section will work fine.

    Even better, use a RAII to thread synchronization. With RAII you write lightweight critical section and/or mutex wrapper classes and use another class which locks on acquisition and automatically unlocks on destruction.

    Here's an example of a RAII implementation to remove items in a queue (of course, inserts and other queue operations would be guarded with the same technique).

    Code:
    void ClearItemQueue( )
    {
      CAutoLockT< CItemQueue > lock( &m_ItemQueue );
    
      while( !m_ItemQueue.empty( ) )
      {
        delete m_ItemQueue.front( );
        m_ItemQueue.pop( );
      }
    }
    You can see a full sample in my reply in this thread: http://www.codeguru.com/forum/showthread.php?p=1688454

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center