Created
January 4, 2015 08:53
-
-
Save JubbaSmail/65bd2fac7a6bd9c5bd08 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <windows.h> | |
int G_counter = 0; | |
//------------------------------------------- | |
// A function that represents Thread number 1 | |
//------------------------------------------- | |
DWORD WINAPI Thread_no_1(LPVOID lpParam) | |
{ | |
// Cast the parameter to the correct | |
// data type passed by callee i.e main() in our case. | |
int Data = *((int*)lpParam); | |
for (int count = 0; count <= 4; count++) | |
{ | |
printf("1- Data: %02d, Global Counter: %02d, Local Counter: %02d, Clock: %09d \n", Data, G_counter++, count, clock()); | |
Sleep(1000);//Sleep for 1 second == 1000 mili-second | |
} | |
return 0; | |
} | |
//------------------------------------------- | |
// A function that represents Thread number 2 | |
//------------------------------------------- | |
DWORD WINAPI Thread_no_2(LPVOID lpParam) | |
{ | |
// Cast the parameter to the correct | |
// data type passed by callee i.e main() in our case. | |
int Data = *((int*)lpParam); | |
for (int count = 0; count <= 7; count++) | |
{ | |
printf("2- Data: %02d, Global Counter: %02d, Local Counter: %02d, Clock: %09d \n", Data, G_counter++, count, clock()); | |
Sleep(1000); | |
} | |
return 0; | |
} | |
//------------------------------------------- | |
// A function that represents Thread number 3 | |
//------------------------------------------- | |
DWORD WINAPI Thread_no_3(LPVOID lpParam) | |
{ | |
// Cast the parameter to the correct | |
// data type passed by callee i.e main() in our case. | |
int Data = *((int*)lpParam); | |
for (int count = 0; count <= 10; count++) | |
{ | |
printf("3- Data: %02d, Global Counter: %02d, Local Counter: %02d, Clock: %09d \n", Data, G_counter++, count, clock()); | |
Sleep(1000); | |
} | |
return 0; | |
} | |
void main() | |
{ | |
// Data of Thread 1 | |
int Data_Of_Thread_1 = 10; | |
// Data of Thread 2 | |
int Data_Of_Thread_2 = 20; | |
// Data of Thread 3 | |
int Data_Of_Thread_3 = 30; | |
// variable to hold handle of Threads | |
HANDLE Handle_Of_Thread_1 = NULL, Handle_Of_Thread_2 = NULL, Handle_Of_Thread_3 = NULL; | |
// Create thread 1. | |
Handle_Of_Thread_1 = CreateThread(NULL,//Security Param | |
0,// default stack size | |
Thread_no_1,// thread function name | |
&Data_Of_Thread_1, // pointer to thread argument | |
0,// 0=run immeditly | CREATE_SUSPENDED | |
NULL // OUT : return thead pointer | |
); | |
// Create thread 2. | |
Handle_Of_Thread_2 = CreateThread(NULL, 0,Thread_no_2, &Data_Of_Thread_2, 0, NULL); | |
// Create thread 3. | |
Handle_Of_Thread_3 = CreateThread(NULL, 0, Thread_no_3, &Data_Of_Thread_3, 0, NULL); | |
// Store Thread handles in Array of Thread | |
// Handles as per the requirement | |
// of WaitForMultipleObjects() | |
HANDLE Array_Of_Thread_Handles[3]; | |
Array_Of_Thread_Handles[0] = Handle_Of_Thread_1; | |
Array_Of_Thread_Handles[1] = Handle_Of_Thread_2; | |
Array_Of_Thread_Handles[2] = Handle_Of_Thread_3; | |
if (Handle_Of_Thread_1 == NULL || Handle_Of_Thread_2 == NULL || Handle_Of_Thread_3 == NULL) | |
return 1; | |
// Wait until all threads have terminated. | |
WaitForMultipleObjects(3, // Num of Thread | |
Array_Of_Thread_Handles, // Thread array | |
TRUE, // wait all ? TRUE | FLASE | |
INFINITE // number of milliseconds to wait | |
); | |
printf("Since All threads executed lets close their handles \n"); | |
// Close all thread handles upon completion. | |
CloseHandle(Handle_Of_Thread_1); | |
CloseHandle(Handle_Of_Thread_2); | |
CloseHandle(Handle_Of_Thread_3); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment