//================================================= file = semaphore1,c ===== //= A simple bare-bones example of Windows semaphores and threads = //=========================================================================== //= Notes: = //= 1) This program demonstrates how a semaphore can be used to make a = //= thread wait for work. Work is generated in the main program. = //= 2) Ignore two compile-time warnings on unreachable code and unused = //= parameter = //=-------------------------------------------------------------------------= //= Example execution: = //= = //= Count = 6 --- 6 5 4 3 2 1 = //= Count = 0 --- = //= Count = 2 --- 2 1 = //= Count = 0 --- = //= Count = 6 --- 6 5 4 3 2 1 = //= Count = 7 --- 7 6 5 4 3 2 1 = //= Count = 5 --- 5 4 3 2 1 = //= Count = 5 --- 5 4 3 2 1 = //= Count = 8 --- 8 7 6 5 4 3 2 1 = //=-------------------------------------------------------------------------= //= Build: bcc32 -WM semaphore1.c = //=-------------------------------------------------------------------------= //= Execute: semaphore1 = //=-------------------------------------------------------------------------= //= Author: Ken Christensen = //= University of South Florida = //= WWW: http://www.csee.usf.edu/~christen = //= Email: christen@csee.usf.edu = //=-------------------------------------------------------------------------= //= History: KJC (07/04/07) - Genesis (from MSDN examples) = //= KJC (02/28/11) - Renamed semaphore.c to semaphore1.c = //=========================================================================== //----- Include files ------------------------------------------------------- #include // Needed for printf() #include // Needed for rand() and exit() #include // Needed for windows stuff //----- Globals ------------------------------------------------------------- HANDLE ghSemaphore; // Windows semaphore int Count; // Global counter variable //----- Function prototypes ------------------------------------------------- DWORD WINAPI ThreadProc(LPVOID); // Thread function //=========================================================================== //= Main program = //=========================================================================== int main(void) { HANDLE aThread; // Thread handle DWORD ThreadID; // Thread ID int i; // Loop counter // Create a semaphore with initial and max counts of 1 ghSemaphore = CreateSemaphore(NULL, 1, 1, NULL); if (ghSemaphore == NULL) { printf("*** ERROR - CreateSemaphore() failed \n"); exit(1); } // Create a thread aThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) ThreadProc, NULL, 0, &ThreadID); if(aThread == NULL) { printf("*** ERROR - CreateThread() failed \n"); exit(1); } // Main loop Count = 0; while(1) { // Create some work for the thread Count = Count + (rand() % 10); printf("\n Count = %d --- ", Count); // Release the semaphore to kick-off the thread ReleaseSemaphore(ghSemaphore, 1, NULL); // Sleep for 1 second Sleep(1000); } // Wait for thread to terminate WaitForMultipleObjects(1, aThread, TRUE, INFINITE); // Close thread handle CloseHandle(aThread); // Close semaphone handle CloseHandle(ghSemaphore); // Return return(0); } //=========================================================================== //= This is is the thread function = //= - Wait for semaphore release to begin work = //=========================================================================== DWORD WINAPI ThreadProc(LPVOID lpParam) { // Loop forever while(1) { // Wait for main program to release the semaphore WaitForSingleObject(ghSemaphore, INFINITE); // Do some work while(Count > 0) { printf("%d ", Count); Count--; } } }