Tuesday 30 April 2013

MultiThreading on Windows

CreateThread
WaitForSingleObject
ExitThread
GetExitCodeThread
http://www.cs.rpi.edu/academics/courses/netprog/WindowsThreads.html
simple example :
#include <iostream>
#include <Windows.h>
using namespace  std;

DWORD WINAPI Thread1(LPVOID lpParameter)
{
 SYSTEMTIME time1;
 WORD flag = 0;
 int count = 0;
 while(true)
 {
  GetLocalTime(&time1);
  if(flag != time1.wSecond)
  {
   count++;
   if(count >60)
   {
     break;
   }
     cout<<time1.wHour<<" : "<< time1.wMinute <<" : "<<time1.wSecond<<endl;
  }
  flag = time1.wSecond;
 }

 return 0;
}

bool  TimerInterrupt(HANDLE& task1)
{

 DWORD Threadid;
  task1 =   CreateThread(
  NULL,
  0,
  Thread1,
  NULL,
  NULL,
  &Threadid
  );
 if(task1 == NULL)
 {
   cout<<"Could not create Thread"<<endl;
   return false ;
 }
 return true;
}

int main()
{
    HANDLE task;
     if (TimerInterrupt(task) == true)
      {
         cout<<"start new thread ."<<endl;
         WaitForSingleObject(task,INFINITE);
         cout<<" close thread ."<<endl;
         CloseHandle(task);
      }
    return 0;
}              //ThongLT-letrthong@gmail.com

No comments:

Post a Comment