Click here to Skip to main content
15,912,977 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionAccessing and displaying semaphore count Pin
coco24322-May-22 1:42
coco24322-May-22 1:42 
Hi,

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

C++
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,
AnswerRe: Accessing and displaying semaphore count Pin
Mircea Neacsu22-May-22 2:02
Mircea Neacsu22-May-22 2:02 
GeneralRe: Accessing and displaying semaphore count Pin
coco24322-May-22 2:48
coco24322-May-22 2:48 
GeneralRe: Accessing and displaying semaphore count Pin
Mircea Neacsu22-May-22 2:51
Mircea Neacsu22-May-22 2:51 
GeneralRe: Accessing and displaying semaphore count Pin
Richard Andrew x6422-May-22 3:04
professionalRichard Andrew x6422-May-22 3:04 
GeneralRe: Accessing and displaying semaphore count Pin
coco24322-May-22 5:07
coco24322-May-22 5:07 
AnswerRe: Accessing and displaying semaphore count Pin
Victor Nijegorodov22-May-22 6:15
Victor Nijegorodov22-May-22 6:15 
GeneralRe: Accessing and displaying semaphore count Pin
coco24322-May-22 7:17
coco24322-May-22 7:17 
QuestionOwnerDraw Combox Pin
ForNow18-May-22 14:28
ForNow18-May-22 14:28 
AnswerRe: OwnerDraw Combox Pin
Victor Nijegorodov18-May-22 21:13
Victor Nijegorodov18-May-22 21:13 
QuestionC++ code for Gomoku Pin
Lesego Molelekoa18-May-22 5:58
Lesego Molelekoa18-May-22 5:58 
AnswerRe: C++ code for Gomoku Pin
Richard MacCutchan18-May-22 6:01
mveRichard MacCutchan18-May-22 6:01 
AnswerRe: C++ code for Gomoku Pin
Victor Nijegorodov18-May-22 9:25
Victor Nijegorodov18-May-22 9:25 
QuestionSecurity Check Stack overflow compiler GS option Pin
ForNow16-May-22 2:16
ForNow16-May-22 2:16 
AnswerRe: Security Check Stack overflow compiler GS option Pin
Victor Nijegorodov16-May-22 3:08
Victor Nijegorodov16-May-22 3:08 
GeneralRe: Security Check Stack overflow compiler GS option Pin
ForNow16-May-22 3:22
ForNow16-May-22 3:22 
QuestionHow can i define cardinalization in c++? Pin
Member 1563777116-May-22 1:38
Member 1563777116-May-22 1:38 
AnswerRe: How can i define cardinalization in c++? Pin
Mircea Neacsu16-May-22 1:54
Mircea Neacsu16-May-22 1:54 
GeneralRe: How can i define cardinalization in c++? Pin
David Crow16-May-22 2:13
David Crow16-May-22 2:13 
AnswerRe: How can i define cardinalization in c++? Pin
Randor 16-May-22 4:25
professional Randor 16-May-22 4:25 
QuestionConverting from MFC to Native Windows C Pin
ForNow12-May-22 12:48
ForNow12-May-22 12:48 
AnswerRe: Converting from MFC to Native Windows C Pin
Richard MacCutchan13-May-22 0:29
mveRichard MacCutchan13-May-22 0:29 
GeneralRe: Converting from MFC to Native Windows C Pin
ForNow13-May-22 1:34
ForNow13-May-22 1:34 
GeneralRe: Converting from MFC to Native Windows C Pin
Richard MacCutchan13-May-22 2:05
mveRichard MacCutchan13-May-22 2:05 
GeneralRe: Converting from MFC to Native Windows C Pin
ForNow13-May-22 3:10
ForNow13-May-22 3:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.