Another Critical Section Program Example
Create a new empty Win32 console application project. Give a suitable project name and change the project location if needed.
Then, add the source file and give it a suitable name.
Next, add the following source code.
// The following program demonstrates an example of using an critical section object
#include <windows.h>
#include <stdio.h>
// Global variable
CRITICAL_SECTION criticalSectionSample;
///////////// Thread Main ///////////////////////
void ThreadMain(char *threadNum)
{
// Waits for ownership of the specified critical section object.
// The function returns when the calling thread is granted ownership.
// This function does not return a value.
wprintf(L\nEnterCriticalSection() - entering the critical section...\n);
EnterCriticalSection(&criticalSectionSample);
wprintf(L%S in critical section, using the shared resource...\n, threadNum);
// Releases ownership of the specified critical section object.
// This function does not return a value.
wprintf(L\nLeaveCriticalSection() - Leaving the critical section...\n);
LeaveCriticalSection(&criticalSectionSample);
}
/////////////Creating A Child//////////////
HANDLE CreateChildTh(char *threadNum)
{
HANDLE hThread;
DWORD dwId;
hThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadMain,(LPVOID)threadNum,0,&dwId);
if(hThread)
{
wprintf(LCreateThread() is OK, ID num %d...\n, dwId);
return hThread;
}
else
{
wprintf(LCreateThread() failed, error %d\n, GetLastError());
return NULL;
}
}
////////////// The main() ///////////////////////
int wmain(void)
{
HANDLE hThreadHandle[3];
DWORD dwEvent, i;
// Initializes a critical section object.
// This function does not return a value.
wprintf(LInitializing the critical section...\n);
InitializeCriticalSection(&criticalSectionSample);
hThreadHandle[0] = CreateChildTh(ChildThread1);
hThreadHandle[1] = CreateChildTh(ChildThread2);
hThreadHandle[2] = CreateChildTh(ChildThread3);
// Waits until one or all of the specified objects are
// in the signaled state or the time-out interval elapses.
dwEvent = WaitForMultipleObjects(3,hThreadHandle,TRUE,INFINITE);
switch (dwEvent)
{
// hThreadHandle[0] was signaled
case WAIT_OBJECT_0 + 0:
// TODO: Perform tasks required by this event
wprintf(LFirst event was signaled.\n);
break;
// hThreadHandle[1] was signaled
case WAIT_OBJECT_0 + 1:
// TODO: Perform tasks required by this event
wprintf(LSecond event was signaled.\n);
break;
// hThreadHandle[2] was signaled
case WAIT_OBJECT_0 + 2:
// TODO: Perform tasks required by this event
wprintf(LThird event was signaled.\n);
break;
case WAIT_TIMEOUT:
wprintf(LWait timed out...\n);
break;
// Return value is invalid.
default:
wprintf(LWait error %d\n, GetLastError());
ExitProcess(0);
}
for(i = 0;i<3;i++)
{
if(CloseHandle(hThreadHandle[i]) != 0)
wprintf(LClosing the hThreadHandle[%d] handle is OK...\n, i);
else
wprintf(LFailed to close the hThreadHandle[%d] handle, error %d...\n, GetLastError());
}
// Releases all resources used by an unowned critical section object.
// This function does not return a value.
wprintf(LDeleteCriticalSection() - Releasing all resources used\n by an unowned critical section object...\n);
DeleteCriticalSection(&criticalSectionSample);
return 0;
}
Build and run the project. The following screenshot is a sample output.