-1
The doubt arose in a college job where we need to make use of threads in c. We have been asked to study the "libpthread" so we can get on with the job. The material I’m finding in my research does not answer my doubts and the official documentation I found somewhat confusing. Consider the following code:
/*
Nesse código bobo, 4 threads são encarregadas de printar sua parte correspondente do vetor, é
um vetor de 20 elementos e cada thread está responsável por 5 deles
*/
#include
#include
#include
#include
typedef struct _lista_numeros {
int *ponteiro_Lista;
int parametro;
} lista_numeros;
void imprime_lista (lista_numeros *lista, int inicio, int fim)
{
int i;
for (i=inicio; iponteiro_Lista[i]);
printf("\n");
}
void * rotina_de_impressao (void *parametro)
{
//Pegando o parametro void que chegou e convertendo para t_lista
lista_numeros *nova_lista = (lista_numeros *) parametro;
switch (nova_lista->parametro)
{
case 1: printf("Thread 1 executando...\n"); imprime_lista(nova_lista, 0, 5); break;
case 2: sleep(20); printf("Thread 2 executando...\n"); imprime_lista(nova_lista, 5, 10); break;
case 3: printf("Thread 3 executando...\n"); imprime_lista(nova_lista, 10, 15); break;
case 4: printf("Thread 4 executando...\n"); imprime_lista(nova_lista, 15, 20); break;
default: printf("Nenhuma thread em trabalho!"); break;
}
}
int main ()
{
//As tais 4 threads responsáveis pela impressão
pthread_t tarefas[4];
lista_numeros primeira_lista;
int lista[20] = {/*Para a thread 1*/60,57,8,77,32, /*Para a thread 2*/98,71,95,2,33,
/*Para a thread 3*/24,68,14,85,36, /*Para a thread 4*/25,88,76,11,34};
primeira_lista.ponteiro_Lista = lista;
primeira_lista.parametro = 1;
pthread_create(&tarefas[0], NULL, rotina_de_impressao, (void *) &primeira_lista);
pthread_join(tarefas[0], NULL);
primeira_lista.parametro = 2;
pthread_create(&tarefas[1], NULL, rotina_de_impressao, (void *) &primeira_lista);
pthread_join(tarefas[1], NULL);
primeira_lista.parametro = 3;
pthread_create(&tarefas[2], NULL, rotina_de_impressao, (void *) &primeira_lista);
pthread_join(tarefas[2], NULL);
primeira_lista.parametro = 4;
pthread_create(&tarefas[3], NULL, rotina_de_impressao, (void *) &primeira_lista);
pthread_join(tarefas[3], NULL);
return 0;
}
Note that in the function "routine_de_impressao", if the value of the parameter is 2, I place a Sleep from 20 to delay thread 2, just to check if threads 3 and 4 would be executed, because that’s what I expected, since the idea of threads is to execute code snippets at the same time, right, but what happened is that the output was the same as not having Sleep there, he first waited for Sleep and then ran threads 3 and 4. I didn’t use loop for in creating threads just for didactics. I tried to take off pthread_join to see if the threads actually ran at the same time, but what happened was a completely uneven execution flow.
Exit with sleep()
and without sleep()
was the same, follows below:
Thread 1 executando...
[60] [57] [8] [77] [32]
Thread 2 executando...
[98] [71] [95] [2] [33]
Thread 3 executando...
[24] [68] [14] [85] [36]
Thread 4 executando...
[25] [88] [76] [11] [34]
How do I actually execute two snippets of code at the same time?
Do not use greetings or greetings in questions, see what kind of behavior is expected from users?
– gleisin-dev