Advertisement
obernardovieira

Timer e Multi-Processo (WINAPI)

Jun 23rd, 2013
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.01 KB | None | 0 0
  1. #include <iostream>
  2. #include <Windows.h>
  3. #include <thread>
  4.  
  5. DWORD WINAPI vaiThread(LPVOID args) {
  6.     Sleep(5000);//o codigo pára apenas neste processo, o outro continua a executar
  7.     static int repeticoes=0;
  8.     int* value = reinterpret_cast<int*>(args);
  9.     printf("\n no thread %d", *value);
  10.     if(!repeticoes) {
  11.         vaiThread(value);//vamos repetir esta parte
  12.     }
  13.     repeticoes++;
  14.     return 1;
  15. }
  16.  
  17. DWORD WINAPI runThread(LPVOID args)
  18. {
  19.     int* value = reinterpret_cast<int*>(args);
  20.     printf("\n in thread %d", *value);
  21.     return 1;
  22. }
  23.  
  24. int main()
  25. {
  26.     DWORD threadId;
  27.     DWORD idthread;
  28.     int valor=5;
  29.     int value = 10;
  30.     HANDLE hThread[2];
  31.  
  32.     hThread[0] = CreateThread( NULL, 0, vaiThread, &valor, 0, &idthread);//cria um processo
  33.     hThread[1] = CreateThread( NULL, 0, runThread, &value, 0, &threadId);//cira outro processo
  34.     Sleep(11000);//os dois processo foram executados, entao vamos para o processo atual para ver o resultado dos outros
  35.     CloseHandle(hThread[0]);
  36.     CloseHandle(hThread[1]);
  37.     system("pause");
  38.     return 0;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement