#include<stdio.h> #include<windows.h> #include<process.h> // needed for _beginthread() CRITICAL_SECTION m_cs; int ThreadOne(void *Param){ int i; //Lock the critical section EnterCriticalSection(&m_cs); for (i=0; i<10; i++){ printf("Function 1 : %d\n", i); } //Release the Critical section LeaveCriticalSection(&m_cs); //Return the thread return 0; } int ThreadTwo(void *Param){ int i; //Lock the critical section EnterCriticalSection(&m_cs); for (i=0; i<10; i++){ printf("Function 2 : %d\n", i); } //Release the Critical section LeaveCriticalSection(&m_cs); //Return the thread return 0; } int main() { // Create the array of Handle HANDLE hThrd[2]; //Initialize the critical section InitializeCriticalSection(&m_cs); // Create threads use CreateThread function with NULL Security hThrd[0] = _beginthread(ThreadOne, 0, (void *)0); hThrd[1] = _beginthread(ThreadTwo, 0, (void *)0); // Wait for the main thread WaitForMultipleObjects(2,hThrd,TRUE,INFINITE); // Delete critical Section DeleteCriticalSection(&m_cs); getchar(); return 0; }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)