Buscar

Escopo de codigo de treads

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

#include <stdio.h>
#include <errno.h>
#include <stddef.h> /* _threadid variable */
#include <process.h> /* _beginthread, _endthread */
#include <time.h> /* time, _ctime */
char listachar[30];
int indice = 0;
int flag1 = 1;
int flag2 = 1;
int flag3 = 1;
int turn = 1;
int delay_s(float tempo)
{
 time_t start, end;
 long unsigned t;
 start = time(NULL);
 do{
 end = time(NULL);
 }while(difftime(end, start)<=tempo);
 return 0;
}
void thread_code_a(void *threadno)
{
 while (1){
 flag1 = 1;
 turn = 1;
 //while((flag2 == 1 || flag3 == 1) && (turn == 1)){}
 while((flag2 == 1 && flag3 == 1) && (turn == 1)){}
 //while((flag2 == 1) && (turn =! 2)){}
 if (indice == 0 || indice == 3 || indice == 6 || indice == 9 ||
 indice == 12 || indice == 15 || indice == 18 || indice == 21 ||
 indice == 24 || indice == 27){
 listachar[indice] = 'a';
 indice++;
 }
 flag1 = 0;
 //delay_s(1);
 if(indice>=27)
 return;
 //delay_s(1);
 return;
 }
}
void thread_code_b(void *threadno)
{
 while (1){
 flag2 = 1;
 turn = 2;
 //while((flag1 == 1 || flag3 == 1) && (turn == 2)){}
 while((flag1 == 1 && flag3 == 1) && (turn == 2)){}
 //while((flag3 == 1) && (turn =! 3)){}
 if (indice == 1 || indice == 4 || indice == 7 || indice == 10 ||
 indice == 13 || indice == 16 || indice == 19 || indice == 22 ||
 indice == 25 || indice == 28){
 listachar[indice] = 'b';
 indice++;
 }
 flag2 = 0;
 //delay_s(1);
 if(indice)
 printf("o vetor 1 e %c\n", listachar[indice]);
 //return;
 //delay_s(0.1);
 }
}
void thread_code_c(void *threadno)
{
 while (1){
 flag3 = 1;
 turn = 3;
 //while((flag1 == 1 || flag2 == 1) && (turn == 3)){}
 while((flag1 == 1 && flag2 == 1) && (turn == 3)){}
 //while((flag1 == 1) && (turn =! 1)){}
 if (indice == 2 || indice == 5 || indice == 8 || indice == 11 ||
 indice == 14 || indice == 17 || indice == 20 || indice == 23 ||
 indice == 26 || indice == 29){
 listachar[indice] = 'c';
 indice++;
 }
 flag3 = 0;
 //delay_s(1);
 if(indice){
 //for(indice = 0; indice < 30; indice++)
 printf("o vetor 2 e %c\n", listachar[indice]);
 //return;
 }
 //delay_s(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");
 #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");
 #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");
}
int main(void)
{
 start_thread();
 printf("Hit ENTER to exit main thread.\n");
 getchar();
 return 0;
}

Teste o Premium para desbloquear

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

Continue navegando