Advertisement
steverobinson

Multi-threading Example || Xoax.net

Feb 1st, 2011
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.99 KB | None | 0 0
  1. #include "windows.h"            //Included to Use Windows Functions
  2. #include <iostream>
  3. #include <cstdio>
  4.  
  5. using namespace std;
  6.  
  7. DWORD WINAPI ThreadFn (LPVOID vpParam);         //Prototype of Function that will contain the code for the new thread
  8. int main()
  9. {
  10.     unsigned int count=0;
  11.  
  12.     DWORD ThreadId;                 //Variable to Hold ThreadID
  13.     HANDLE hThread=CreateThread(0,0,ThreadFn,&count,0,&ThreadId);       //SystemCall to Create Thread
  14.  
  15.     //Loop Until User Presses q
  16.     char ch=' ';
  17.     while(ch!='q')
  18.     {
  19.         cout<<endl<<count;
  20.         ch=(char)getchar();
  21.  
  22.     }
  23.  
  24.  
  25.  
  26.     return 0;
  27. }
  28.  
  29. //Thread Function Definition
  30. DWORD WINAPI ThreadFn (LPVOID vpParam)
  31. {
  32.     unsigned int &counter=*((unsigned int*)vpParam);        //Dereferencing the void pointer after casting to unsigned int and assigning it to a local reference
  33.     cout<<"\nInside New Thread!";
  34.     while(counter<0xFFFFFFFF)
  35.         counter++;
  36.     return 0;                   //End of Thread
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement