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. Mutexes and WaitForSingleObject -> doesn´t wait ??

Mutexes and WaitForSingleObject -> doesn´t wait ??

Scheduled Pinned Locked Moved C / C++ / MFC
questionsecuritylounge
5 Posts 3 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.
  • S Offline
    S Offline
    Souldrift
    wrote on last edited by
    #1

    Hello, I just played around with mutexes and wrote the following piece of code:

    HANDLE hMutex = CreateMutex(NULL, // no security descriptor needed in the beginning
    TRUE, // not initial owner
    "test"); // mutex name (hopefully unique)

    DWORD err = WaitForSingleObject(hMutex, INFINITE);
    if( err == WAIT_FAILED )
    printf( "This shouldn´t happen." );
    else
    {
    err = WaitForSingleObject(hMutex, INFINITE);
    if( err == WAIT_FAILED )
    printf( "This shouldn´t happen." );
    else
    printf( "This shouldn´t happen either." );
    }

    No I don´t understand why the second WaitForSingleObject()-call doesn´t block. Since I said to wait indefinitely, shouldn´t it do so because the mutex object is already in use? I guess, I have some general trouble understanding the mechanism here. How do I get the mutex to lock a code area up? Souldrift

    M C 2 Replies Last reply
    0
    • S Souldrift

      Hello, I just played around with mutexes and wrote the following piece of code:

      HANDLE hMutex = CreateMutex(NULL, // no security descriptor needed in the beginning
      TRUE, // not initial owner
      "test"); // mutex name (hopefully unique)

      DWORD err = WaitForSingleObject(hMutex, INFINITE);
      if( err == WAIT_FAILED )
      printf( "This shouldn´t happen." );
      else
      {
      err = WaitForSingleObject(hMutex, INFINITE);
      if( err == WAIT_FAILED )
      printf( "This shouldn´t happen." );
      else
      printf( "This shouldn´t happen either." );
      }

      No I don´t understand why the second WaitForSingleObject()-call doesn´t block. Since I said to wait indefinitely, shouldn´t it do so because the mutex object is already in use? I guess, I have some general trouble understanding the mechanism here. How do I get the mutex to lock a code area up? Souldrift

      M Offline
      M Offline
      Michael Schubert
      wrote on last edited by
      #2

      Whay don't you use GetLastError() when the return value is WAIT_FAILED as the documentation suggests?

      S 1 Reply Last reply
      0
      • M Michael Schubert

        Whay don't you use GetLastError() when the return value is WAIT_FAILED as the documentation suggests?

        S Offline
        S Offline
        Souldrift
        wrote on last edited by
        #3

        There is no error. The return value both times is 0. Which is fine with the first call, and I thought was odd with the second. But I just read somewhere else, that mutexes under windows don´t block inside the same thread. So that might be it. Souldrift

        1 Reply Last reply
        0
        • S Souldrift

          Hello, I just played around with mutexes and wrote the following piece of code:

          HANDLE hMutex = CreateMutex(NULL, // no security descriptor needed in the beginning
          TRUE, // not initial owner
          "test"); // mutex name (hopefully unique)

          DWORD err = WaitForSingleObject(hMutex, INFINITE);
          if( err == WAIT_FAILED )
          printf( "This shouldn´t happen." );
          else
          {
          err = WaitForSingleObject(hMutex, INFINITE);
          if( err == WAIT_FAILED )
          printf( "This shouldn´t happen." );
          else
          printf( "This shouldn´t happen either." );
          }

          No I don´t understand why the second WaitForSingleObject()-call doesn´t block. Since I said to wait indefinitely, shouldn´t it do so because the mutex object is already in use? I guess, I have some general trouble understanding the mechanism here. How do I get the mutex to lock a code area up? Souldrift

          C Offline
          C Offline
          CPallini
          wrote on last edited by
          #4

          Souldrift wrote:

          No I don´t understand why the second WaitForSingleObject()-call doesn´t block. Since I said to wait indefinitely, shouldn´t it do so because the mutex object is already in use?

          Souldrift wrote:

          I guess, I have some general trouble understanding the mechanism here.

          You're missing this ([^])

          The thread that owns a mutex can specify the same mutex in repeated wait function calls without blocking its execution. Typically, you would not wait repeatedly for the same mutex, but this mechanism prevents a thread from deadlocking itself while waiting for a mutex that it already owns. However, to release its ownership, the thread must call ReleaseMutex once for each time that the mutex satisfied a wait.

          :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          S 1 Reply Last reply
          0
          • C CPallini

            Souldrift wrote:

            No I don´t understand why the second WaitForSingleObject()-call doesn´t block. Since I said to wait indefinitely, shouldn´t it do so because the mutex object is already in use?

            Souldrift wrote:

            I guess, I have some general trouble understanding the mechanism here.

            You're missing this ([^])

            The thread that owns a mutex can specify the same mutex in repeated wait function calls without blocking its execution. Typically, you would not wait repeatedly for the same mutex, but this mechanism prevents a thread from deadlocking itself while waiting for a mutex that it already owns. However, to release its ownership, the thread must call ReleaseMutex once for each time that the mutex satisfied a wait.

            :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            S Offline
            S Offline
            Souldrift
            wrote on last edited by
            #5

            Yes, that was exactly it. Just found out a minute ago. Thanks a lot. Just tested it with threads and it seems to work allright :). Good day all. Souldrift

            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