Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // aula_thread.cpp : Defines the entry point for the console application.
- //
- #include<Windows.h>
- #include<process.h>
- #include<stdio.h>
- #include<stdlib.h>
- #include<time.h>
- #include<iostream>
- #include<vector>
- #define dim 1000
- #define threads 4
- typedef struct{
- int li, lf, ci, cf, indice;
- }par;
- int mat[dim][dim];
- int total_numeros_primos = 0;
- HANDLE mSC1, hMutexVideo;
- void preencher_matriz();
- void qtd_primos(void *parThread);
- bool eh_primo(int x);
- void SetParametros(par p[]);
- void CriarThreads(HANDLE idThread[], par parametro[], DWORD ThreadIdentifier[]);
- void SetPrioridade(HANDLE idThread[]);
- void MostrarPrioridade(HANDLE idThread[]);
- void PressEnter();
- void IniciarThreads(HANDLE idThread[]);
- void FecharThread(HANDLE idThread[]);
- int main()
- {
- srand (12);
- DWORD ThreadIdentifier[threads];
- HANDLE idThread[threads];
- par parametro[threads];
- clock_t inicio, fim;
- int total_primos = 0;
- inicio = clock();
- //criando Mutex
- mSC1 = CreateMutex(NULL, FALSE, NULL);
- hMutexVideo = CreateMutex(NULL, FALSE, NULL);
- preencher_matriz();
- SetParametros(parametro);
- CriarThreads(idThread, parametro, ThreadIdentifier);
- SetPrioridade(idThread);
- MostrarPrioridade(idThread);
- PressEnter();
- IniciarThreads(idThread);
- WaitForMultipleObjects(threads, idThread, TRUE, INFINITE);
- FecharThread(idThread);
- /*WaitForSingleObject(idThread[1], INFINITE);
- for(int i = 0; i < threads; i++)
- {
- total_primos += parametro[i].soma;
- }
- printf("Qtd de primos: %d\n", total_primos);*/
- printf("total numeros primos: %d\n", total_numeros_primos);
- fim = clock();
- double time = ((double) (fim - inicio) ) / CLOCKS_PER_SEC;
- printf("tempo: %lf\n", time);
- system("pause");
- return 0;
- }
- void preencher_matriz()
- {
- for(int i = 0; i < dim; i++)
- {
- for(int j = 0; j < dim; j++)
- {
- mat[i][j] = rand()%1000000;
- }
- }
- }
- void qtd_primos(void *parThread)
- {
- par *parFuncao = (par *) parThread;
- WaitForSingleObject(hMutexVideo, INFINITE);
- printf("linha_inicial: %d\tlinha final: %d\n", parFuncao->li, parFuncao->lf);
- printf("coluna_inicial: %d\tcoluna final: %d\n\n", parFuncao->ci, parFuncao->cf);
- ReleaseMutex(hMutexVideo);
- //printf("Starting thread %d\n", parFuncao->indice);
- for(int i = parFuncao->li; i < parFuncao->lf; i++)
- {
- for(int j = parFuncao->ci; j < parFuncao->cf; j++)
- {
- // inicio seção crítica
- WaitForSingleObject(mSC1, INFINITE);
- total_numeros_primos += eh_primo(mat[i][j]);
- //parFuncao->soma += eh_primo(mat[i][j]);
- ReleaseMutex(mSC1);
- // fim seção crítica
- }
- }
- }
- bool eh_primo(int x)
- {
- if(x % 2 == 0 && x!=2)
- {
- return(0);
- }
- else if(x == 2)
- {
- return(1);
- }
- else
- {
- int aux = 0;
- for(int i = 1; i <= x; i+=2)
- {
- if(x % i == 0)
- {
- aux++;
- }
- if(aux >= 3)
- {
- return(0);
- }
- }
- if(aux == 2)
- {
- return(1);
- }
- else
- {
- return(0);
- }
- }
- }
- void SetParametros(par p[])
- {
- for(int i = 0; i < threads; i++)
- {
- p[i].li = 0;
- p[i].lf = dim;
- p[i].ci = 0 + (dim/threads)*i;
- p[i].cf = (dim/threads) + (dim/threads)*i;
- //p[i].soma = 0;
- p[i].indice = i;
- }
- }
- void CriarThreads(HANDLE idThread[], par parametro[], DWORD ThreadIdentifier[])
- {
- for(int i = 0; i < threads; i++)
- {
- idThread[i] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)qtd_primos, ¶metro[i], CREATE_SUSPENDED, &ThreadIdentifier[i]);
- }
- }
- void SetPrioridade(HANDLE idThread[])
- {
- SetThreadPriority(idThread[0], THREAD_PRIORITY_BELOW_NORMAL );
- SetThreadPriority(idThread[2], THREAD_PRIORITY_HIGHEST );
- }
- void MostrarPrioridade(HANDLE idThread[])
- {
- WaitForSingleObject(hMutexVideo, INFINITE);
- printf("\n");
- for(int i = 0; i < threads; i++)
- {
- printf("thread %d priority %d\n", i, GetThreadPriority(idThread[i]));
- }
- printf("\n");
- ReleaseMutex(hMutexVideo);
- }
- void PressEnter()
- {
- WaitForSingleObject(hMutexVideo, INFINITE);
- printf("Press enter to start the threads ");
- getchar();
- ReleaseMutex(hMutexVideo);
- }
- void IniciarThreads(HANDLE idThread[])
- {
- for(int i = 0; i < threads; i++)
- {
- ResumeThread(idThread[i]);
- }
- }
- void FecharThread(HANDLE idThread[])
- {
- for(int i = 0; i < threads; i++)
- {
- CloseHandle(idThread[i]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement