Accessing and displaying semaphore count
-
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,
-
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,
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
-
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
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,
-
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,
Hmm,
dwSCount
vsdwSemCount
. Could that be a typo?Mircea
-
Hmm,
dwSCount
vsdwSemCount
. Could that be a typo?Mircea
Good catch!
The difficult we do right away... ...the impossible takes slightly longer.
-
Hmm,
dwSCount
vsdwSemCount
. Could that be a typo?Mircea
-
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,
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.
-
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.