Buscar

Compara threads

Esta é uma pré-visualização de arquivo. Entre para ver o arquivo original

#include <stdio.h>
#include <errno.h>
#include <stddef.h>
#include <process.h>
int flag_F = 0; //habilita o processamento da thread_code_f
int V_compara [6]; //Vetor para guradar os valores de PID das threads criadas
int i=1;
int k=0;
int j=0;
void thread_code_a(void *threadno)
{
 while(1){}
}
 
void thread_code_b(void *threadno)
{
 while(1){}
}
 
void thread_code_c(void *threadno)
{
 while(1){}
}
void thread_code_d(void *threadno)
{
 while(1){} 
}
void thread_code_e(void *threadno)
{
 while(1){} 
}
void thread_code_f(void *threadno)
{
 while(flag_F==0){}
 printf("\n Os PIDs relacionados as threads criadas sao: \n\n");
 for(j= 0; j<= 6; j++)
 { 
 printf("%d ", V_compara [j]);
 }
}
/* ****
 Teste para identificar se o PID das threads se repete
*/
void compara_thread()
{
 for(j= 0; j<= 4; j++)
 {
 for(k=j+1; k<=5; k++)
 {
 if ( V_compara [j]==V_compara [k]){ 
 i=1; //Se o PID for igual, retorno a criação de threads 
 return;
 }else{ 
 i=0;//Se os PID são diferentes, executar a thread 6
 flag_F = 1; 
 }
 }
 } 
}
void start_thread()
{
 int thread_id;
 
 #if defined(_WIN32_)
 if ((thread_id= _beginthread(thread_code_a,4096, NULL))==(unsigned long)-1)
 #else
 if ((thread_id= _beginthread(thread_code_a,4096, NULL))== -1)
 #endif 
 {
 printf("Unable to create thread a.\n");
 return;
 }
 printf("Created thread a.\n");
 V_compara [0]= thread_id;
 
 
 
 #if defined(_WIN32_)
 if ((thread_id= _beginthread(thread_code_b,4096, NULL))==(unsigned long)-1)
 #else
 if ((thread_id= _beginthread(thread_code_b,4096, NULL))== -1)
 #endif 
 {
 printf("Unable to create thread b.\n");
 return;
 }
 printf("Created thread b.\n");
 V_compara [1]= thread_id;
 
 #if defined(_WIN32_)
 if ((thread_id= _beginthread(thread_code_c,4096, NULL))==(unsigned long)-1)
 #else
 if ((thread_id= _beginthread(thread_code_c,4096, NULL))== -1)
 #endif 
 {
 printf("Unable to create thread c.\n");
 return;
 }
 printf("Created thread c.\n");
 V_compara [2]= thread_id;
 
 #if defined(_WIN32_)
 if ((thread_id= _beginthread(thread_code_d,4096, NULL))==(unsigned long)-1)
 #else
 if ((thread_id= _beginthread(thread_code_d,4096, NULL))== -1)
 #endif 
 {
 printf("Unable to create thread d.\n");
 return;
 }
 printf("Created thread d.\n");
 V_compara [3]= thread_id;
 
 
 #if defined(_WIN32_)
 if ((thread_id= _beginthread(thread_code_e,4096, NULL))==(unsigned long)-1)
 #else
 if ((thread_id= _beginthread(thread_code_e,4096, NULL))== -1)
 #endif 
 {
 printf("Unable to create thread e.\n");
 return;
 }
 printf("Created thread e.\n");
 V_compara [4]= thread_id;
 
 
 #if defined(_WIN32_)
 if ((thread_id= _beginthread(thread_code_f,4096, NULL))==(unsigned long)-1)
 #else
 if ((thread_id= _beginthread(thread_code_f,4096, NULL))== -1)
 #endif 
 {
 printf("Unable to create thread f.\n");
 return;
 }
 printf("Created thread f.\n");
 V_compara [5]= thread_id;
 compara_thread();
 
}
 
int main(void)
{
 printf("------------------------------------------------------------------");
 printf("\n\n CRIACAO DE THREADS\n\n");
 printf("------------------------------------------------------------------\n");
 if (i==1)
 {
 start_thread();
 }
 getchar();
 return 0;
 }

Teste o Premium para desbloquear

Aproveite todos os benefícios por 3 dias sem pagar! 😉
Já tem cadastro?

Continue navegando