Advertisement
andresilva5284

Threads em C

Dec 12th, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.61 KB | None | 0 0
  1. /* Este programa exemplifica como fazer a execução de múltiplas funções sequenciais. */
  2.  
  3. #include <windows.h>
  4. #include <stdio.h>
  5.  
  6. #define TEMPO_SLEEP 5
  7.  
  8. HANDLE console; // Handle do console
  9.  
  10. CRITICAL_SECTION video_CriticalSection; // Garantir acesso exclusivo ao video
  11.  
  12. void GotoXY(int x, int y )
  13. {
  14.    COORD CurPos;
  15.    CurPos.X = x;
  16.    CurPos.Y = y;
  17.    SetConsoleCursorPosition(console, CurPos);
  18. }
  19.  
  20. void printJogoDaVelha()
  21. {
  22.      int i, j;
  23.      for (i=5; i<25; i++)
  24.         for (j=3; j<23; j++)
  25.         {
  26.            EnterCriticalSection(&video_CriticalSection);
  27.            GotoXY(i, j);
  28.            printf("#");
  29.            LeaveCriticalSection(&video_CriticalSection);    
  30.            Sleep(TEMPO_SLEEP);
  31.         }
  32. }
  33.  
  34. void printEstrela()
  35. {
  36.      int i, j;
  37.      for (i=25; i<45; i++)
  38.         for (j=3; j<23; j++)
  39.  
  40.         {
  41.            EnterCriticalSection(&video_CriticalSection);
  42.            GotoXY(i, j);
  43.            printf("*");
  44.            LeaveCriticalSection(&video_CriticalSection);    
  45.            Sleep(TEMPO_SLEEP);
  46.         }
  47. }
  48.  
  49. void printArroba()
  50. {
  51.      int i, j;
  52.      for (i=45; i<65; i++)
  53.         for (j=3; j<23; j++)
  54.         {
  55.            EnterCriticalSection(&video_CriticalSection);
  56.            GotoXY(i, j);
  57.            printf("@");
  58.            LeaveCriticalSection(&video_CriticalSection);    
  59.            Sleep(TEMPO_SLEEP);
  60.         }
  61. }
  62.  
  63. int main()
  64. {
  65.    DWORD tid;
  66.    
  67.    __int64 freq,start,stop;
  68.    double tempo;
  69.  
  70.    
  71.    InitializeCriticalSection(&video_CriticalSection);
  72.    
  73.    // Handle da consola  
  74.    console = GetStdHandle( STD_OUTPUT_HANDLE );
  75.    // Inicia titulo do console
  76.    SetConsoleTitle( "Threads Concorrentes - Prof. Cristiano Lino Felicio" );
  77.  
  78.    EnterCriticalSection(&video_CriticalSection);
  79.    GotoXY(1, 0);
  80.    printf("--> Exemplo: 3 funcoes (# -- * -- @) executando sequencialmete.\n");
  81.    GotoXY(1, 1);
  82.    printf("--> Pressione alguma tecla para prosseguir ...\n");
  83.    LeaveCriticalSection(&video_CriticalSection);    
  84.    getch();
  85.    
  86.    QueryPerformanceFrequency((LARGE_INTEGER *)&freq);
  87.    QueryPerformanceCounter((LARGE_INTEGER *)&start);
  88.  
  89.    printJogoDaVelha();
  90.    printEstrela();
  91.    printArroba();
  92.  
  93.    QueryPerformanceCounter((LARGE_INTEGER *)&stop);
  94.    tempo = ((double)stop-(double)start) / (double)freq;
  95.    
  96.    EnterCriticalSection(&video_CriticalSection);
  97.    GotoXY(5,23);
  98.    printf("Tempo Total = %f --- ", tempo);
  99.    printf("Pressione alguma tecla para sair.\n");
  100.    LeaveCriticalSection(&video_CriticalSection);
  101.  
  102.    getch();
  103.    
  104.    CloseHandle(console);
  105.    DeleteCriticalSection(&video_CriticalSection);
  106.    return 0;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement