Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Confused about Mutex?

Confused about Mutex?

Scheduled Pinned Locked Moved C / C++ / MFC
testingbeta-testingtutorialquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nde_plume
    wrote on last edited by
    #1

    I am confused how to get mutex locks to work correctly. As part of my testing I wrote the following code: HANDLE hMutex = CreateMutex(NULL, FALSE, "ABC"); int result = WaitForSingleObject(hMutex, INFINITE); int result2 = WaitForSingleObject(hMutex, INFINITE); The first line creates a mutex, then the next line "locks" the mutex, then the next line waits for and tries to relock the mutex. It seems to me that the second Wait should never return (since the Mutex is already locked.) However, both Waits return immediately with the return code 0 (== WAIT_OBJECT_0). What am I missing?

    W 1 Reply Last reply
    0
    • N nde_plume

      I am confused how to get mutex locks to work correctly. As part of my testing I wrote the following code: HANDLE hMutex = CreateMutex(NULL, FALSE, "ABC"); int result = WaitForSingleObject(hMutex, INFINITE); int result2 = WaitForSingleObject(hMutex, INFINITE); The first line creates a mutex, then the next line "locks" the mutex, then the next line waits for and tries to relock the mutex. It seems to me that the second Wait should never return (since the Mutex is already locked.) However, both Waits return immediately with the return code 0 (== WAIT_OBJECT_0). What am I missing?

      W Offline
      W Offline
      Waldermort
      wrote on last edited by
      #2

      Your first line creates a mutex and your current thread owns it. Your current thread will continue owning it until you call ReleaseMutex(). In your second thread, WaitForSingleObject() will wait until the first thread calls RealeaseMutex(), after the first thread has released it, the second thread will own it. If the first thread wants to own it again, it must wait until the second thread releases it. understand? It doesn't work in your example because it's all in the same thread.

      N 1 Reply Last reply
      0
      • W Waldermort

        Your first line creates a mutex and your current thread owns it. Your current thread will continue owning it until you call ReleaseMutex(). In your second thread, WaitForSingleObject() will wait until the first thread calls RealeaseMutex(), after the first thread has released it, the second thread will own it. If the first thread wants to own it again, it must wait until the second thread releases it. understand? It doesn't work in your example because it's all in the same thread.

        N Offline
        N Offline
        nde_plume
        wrote on last edited by
        #3

        Yes, I get it, I tried in a separate thread and it works as expected. Thanks for your help.

        W 1 Reply Last reply
        0
        • N nde_plume

          Yes, I get it, I tried in a separate thread and it works as expected. Thanks for your help.

          W Offline
          W Offline
          Waldermort
          wrote on last edited by
          #4

          welcome

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups