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. Accessing and displaying semaphore count

Accessing and displaying semaphore count

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestionannouncement
8 Posts 4 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.
  • C Offline
    C Offline
    coco243
    wrote on last edited by
    #1

    Hi, In my trying on understanding semaphores I am looking on the following example:

    DWORD WINAPI ChildThreadProc( HWND hWnd )
    {
    TCHAR szBuffer[256]; // buffer
    DWORD dwSemCount = 0; // printing semaphore count
    HANDLE hSemaphore = OpenSemaphore( SYNCHRONIZE, FALSE, "TEST SEMAPHORE");
    wsprintf(szBuffer, "Thread %x waiting for semaphore %x", GetCurrentThreadId(), hSemaphore );
    SendMessage(hWnd, WM_USER, 0, (LPARAM)szBuffer);
    // Check for signaled semaphore
    WaitForSingleObject( hSemaphore, INFINITE );
    wsprintf(szBuffer, "Thread %x got semaphore", GetCurrentThreadId() );
    SendMessage(hWnd, WM_USER, 0, (LPARAM)szBuffer);
    Sleep(5000);
    //dwSCount = dwSemCount;
    //Release semaphore
    ReleaseSemaphore( hSemaphore, 1, (LPLONG)&dwSCount );
    wsprintf( szBuffer, "Thread %x is done with semaphore. Its count was %ld.", GetCurrentThreadId(), dwSemCount );
    SendMessage( hWnd, WM_USER, 0, (LPARAM)szBuffer );
    CloseHandle( hSemaphore );
    ExitThread ( TRUE );
    }

    And I am having 2 problems here: -first, the dwSemCount value si always printed as 0, even if the semaphore works fine -second, incrementing the counter with ReleaseSemaphore function from inside of thread doesn't working, I can't explain why. Can you please help me with some advices? Thank you very much,

    Mircea NeacsuM V 2 Replies Last reply
    0
    • C coco243

      Hi, In my trying on understanding semaphores I am looking on the following example:

      DWORD WINAPI ChildThreadProc( HWND hWnd )
      {
      TCHAR szBuffer[256]; // buffer
      DWORD dwSemCount = 0; // printing semaphore count
      HANDLE hSemaphore = OpenSemaphore( SYNCHRONIZE, FALSE, "TEST SEMAPHORE");
      wsprintf(szBuffer, "Thread %x waiting for semaphore %x", GetCurrentThreadId(), hSemaphore );
      SendMessage(hWnd, WM_USER, 0, (LPARAM)szBuffer);
      // Check for signaled semaphore
      WaitForSingleObject( hSemaphore, INFINITE );
      wsprintf(szBuffer, "Thread %x got semaphore", GetCurrentThreadId() );
      SendMessage(hWnd, WM_USER, 0, (LPARAM)szBuffer);
      Sleep(5000);
      //dwSCount = dwSemCount;
      //Release semaphore
      ReleaseSemaphore( hSemaphore, 1, (LPLONG)&dwSCount );
      wsprintf( szBuffer, "Thread %x is done with semaphore. Its count was %ld.", GetCurrentThreadId(), dwSemCount );
      SendMessage( hWnd, WM_USER, 0, (LPARAM)szBuffer );
      CloseHandle( hSemaphore );
      ExitThread ( TRUE );
      }

      And I am having 2 problems here: -first, the dwSemCount value si always printed as 0, even if the semaphore works fine -second, incrementing the counter with ReleaseSemaphore function from inside of thread doesn't working, I can't explain why. Can you please help me with some advices? Thank you very much,

      Mircea NeacsuM Online
      Mircea NeacsuM Online
      Mircea Neacsu
      wrote on last edited by
      #2

      Thread doesn’t have enough access rights to semaphore. It would need SEMAPHORE_MODIFY_STATE. See Synchronization Object Security and Access Rights - Win32 apps | Microsoft Docs[^] Checking return value of ReleaseSemaphore would show that it fails.

      Mircea

      C 1 Reply Last reply
      0
      • Mircea NeacsuM Mircea Neacsu

        Thread doesn’t have enough access rights to semaphore. It would need SEMAPHORE_MODIFY_STATE. See Synchronization Object Security and Access Rights - Win32 apps | Microsoft Docs[^] Checking return value of ReleaseSemaphore would show that it fails.

        Mircea

        C Offline
        C Offline
        coco243
        wrote on last edited by
        #3

        Thank you Mircea, I had changed the access rights for OpenSemaphore and the ReleaseSemaphore is working now. Please help me and with the displaying of the semaphore counter:

        ReleaseSemaphore( hSemaphore, 1, (LPLONG)&dwSCount );
        wsprintf( szBuffer, "Thread %x is done with semaphore. Its count was %ld.", GetCurrentThreadId(), dwSemCount );
        SendMessage( hWnd, WM\_USER, 0, (LPARAM)szBuffer );
        

        The displayed value of the dwSemCount is always 0, but it should be equal with the initial value, or with the decremented value, where is the mistake or where I am wrong? Thank you,

        Mircea NeacsuM 1 Reply Last reply
        0
        • C coco243

          Thank you Mircea, I had changed the access rights for OpenSemaphore and the ReleaseSemaphore is working now. Please help me and with the displaying of the semaphore counter:

          ReleaseSemaphore( hSemaphore, 1, (LPLONG)&dwSCount );
          wsprintf( szBuffer, "Thread %x is done with semaphore. Its count was %ld.", GetCurrentThreadId(), dwSemCount );
          SendMessage( hWnd, WM\_USER, 0, (LPARAM)szBuffer );
          

          The displayed value of the dwSemCount is always 0, but it should be equal with the initial value, or with the decremented value, where is the mistake or where I am wrong? Thank you,

          Mircea NeacsuM Online
          Mircea NeacsuM Online
          Mircea Neacsu
          wrote on last edited by
          #4

          Hmm, dwSCount vs dwSemCount. Could that be a typo?

          Mircea

          Richard Andrew x64R C 2 Replies Last reply
          0
          • Mircea NeacsuM Mircea Neacsu

            Hmm, dwSCount vs dwSemCount. Could that be a typo?

            Mircea

            Richard Andrew x64R Offline
            Richard Andrew x64R Offline
            Richard Andrew x64
            wrote on last edited by
            #5

            Good catch!

            The difficult we do right away... ...the impossible takes slightly longer.

            1 Reply Last reply
            0
            • Mircea NeacsuM Mircea Neacsu

              Hmm, dwSCount vs dwSemCount. Could that be a typo?

              Mircea

              C Offline
              C Offline
              coco243
              wrote on last edited by
              #6

              Sorry, I made some test with SCount as global variable because of the problems, but yes, I had replaced this variable and is ok now. Thank you very much,

              1 Reply Last reply
              0
              • C coco243

                Hi, In my trying on understanding semaphores I am looking on the following example:

                DWORD WINAPI ChildThreadProc( HWND hWnd )
                {
                TCHAR szBuffer[256]; // buffer
                DWORD dwSemCount = 0; // printing semaphore count
                HANDLE hSemaphore = OpenSemaphore( SYNCHRONIZE, FALSE, "TEST SEMAPHORE");
                wsprintf(szBuffer, "Thread %x waiting for semaphore %x", GetCurrentThreadId(), hSemaphore );
                SendMessage(hWnd, WM_USER, 0, (LPARAM)szBuffer);
                // Check for signaled semaphore
                WaitForSingleObject( hSemaphore, INFINITE );
                wsprintf(szBuffer, "Thread %x got semaphore", GetCurrentThreadId() );
                SendMessage(hWnd, WM_USER, 0, (LPARAM)szBuffer);
                Sleep(5000);
                //dwSCount = dwSemCount;
                //Release semaphore
                ReleaseSemaphore( hSemaphore, 1, (LPLONG)&dwSCount );
                wsprintf( szBuffer, "Thread %x is done with semaphore. Its count was %ld.", GetCurrentThreadId(), dwSemCount );
                SendMessage( hWnd, WM_USER, 0, (LPARAM)szBuffer );
                CloseHandle( hSemaphore );
                ExitThread ( TRUE );
                }

                And I am having 2 problems here: -first, the dwSemCount value si always printed as 0, even if the semaphore works fine -second, incrementing the counter with ReleaseSemaphore function from inside of thread doesn't working, I can't explain why. Can you please help me with some advices? Thank you very much,

                V Offline
                V Offline
                Victor Nijegorodov
                wrote on last edited by
                #7

                You should avoid using SendMessage form a worker to a main thread. It could lead to a deadlock. In most cases PostMessage is more preferable.

                C 1 Reply Last reply
                0
                • V Victor Nijegorodov

                  You should avoid using SendMessage form a worker to a main thread. It could lead to a deadlock. In most cases PostMessage is more preferable.

                  C Offline
                  C Offline
                  coco243
                  wrote on last edited by
                  #8

                  Noted, Thank you,

                  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