Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Este programa exemplifica como fazer a execução de múltiplas funções sequenciais. */
- #include <windows.h>
- #include <stdio.h>
- #define TEMPO_SLEEP 5
- HANDLE console; // Handle do console
- CRITICAL_SECTION video_CriticalSection; // Garantir acesso exclusivo ao video
- void GotoXY(int x, int y )
- {
- COORD CurPos;
- CurPos.X = x;
- CurPos.Y = y;
- SetConsoleCursorPosition(console, CurPos);
- }
- void printJogoDaVelha()
- {
- int i, j;
- for (i=5; i<25; i++)
- for (j=3; j<23; j++)
- {
- EnterCriticalSection(&video_CriticalSection);
- GotoXY(i, j);
- printf("#");
- LeaveCriticalSection(&video_CriticalSection);
- Sleep(TEMPO_SLEEP);
- }
- }
- void printEstrela()
- {
- int i, j;
- for (i=25; i<45; i++)
- for (j=3; j<23; j++)
- {
- EnterCriticalSection(&video_CriticalSection);
- GotoXY(i, j);
- printf("*");
- LeaveCriticalSection(&video_CriticalSection);
- Sleep(TEMPO_SLEEP);
- }
- }
- void printArroba()
- {
- int i, j;
- for (i=45; i<65; i++)
- for (j=3; j<23; j++)
- {
- EnterCriticalSection(&video_CriticalSection);
- GotoXY(i, j);
- printf("@");
- LeaveCriticalSection(&video_CriticalSection);
- Sleep(TEMPO_SLEEP);
- }
- }
- int main()
- {
- DWORD tid;
- __int64 freq,start,stop;
- double tempo;
- InitializeCriticalSection(&video_CriticalSection);
- // Handle da consola
- console = GetStdHandle( STD_OUTPUT_HANDLE );
- // Inicia titulo do console
- SetConsoleTitle( "Threads Concorrentes - Prof. Cristiano Lino Felicio" );
- EnterCriticalSection(&video_CriticalSection);
- GotoXY(1, 0);
- printf("--> Exemplo: 3 funcoes (# -- * -- @) executando sequencialmete.\n");
- GotoXY(1, 1);
- printf("--> Pressione alguma tecla para prosseguir ...\n");
- LeaveCriticalSection(&video_CriticalSection);
- getch();
- QueryPerformanceFrequency((LARGE_INTEGER *)&freq);
- QueryPerformanceCounter((LARGE_INTEGER *)&start);
- printJogoDaVelha();
- printEstrela();
- printArroba();
- QueryPerformanceCounter((LARGE_INTEGER *)&stop);
- tempo = ((double)stop-(double)start) / (double)freq;
- EnterCriticalSection(&video_CriticalSection);
- GotoXY(5,23);
- printf("Tempo Total = %f --- ", tempo);
- printf("Pressione alguma tecla para sair.\n");
- LeaveCriticalSection(&video_CriticalSection);
- getch();
- CloseHandle(console);
- DeleteCriticalSection(&video_CriticalSection);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement