I want to insert pthread_join function on the following code to terminate my threads and get the variables value updated. After that my idea was to make a variable to add the new values I got from the threads and print it out. I have the following code, that seems to be working fine, but I want to update the variables a and b directly without any extra variable (I don't want to use aux_soma and aux_multiplicacao). If I try to update them directly, they get their value set to 0 (mostly variable a). Is there any way to do it the way I want? The code is written in portuguese, sorry about that, I hope you manage to understand it anyways.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>
void *soma(void *valor)
{
int a = (intptr_t) valor;
a = 10 + a;
//printf("A thread soma terminou.\n");
pthread_exit((void*)(intptr_t)a);
}
void *multiplicacao(void *valor)
{
int a = (intptr_t) valor;
a = 10 * a;
//printf("A thread multiplicacao terminou.\n");
pthread_exit((void *)(intptr_t)a);
}
int main()
{
pthread_t p, t;
int a = 5, b = 5;
int aux_soma, aux_multiplicacao; // Variáveis auxiliares para evitar erros
printf("\n\n"); // Duas linhas em branco; Para ficar separado e mais apresentável
// Estava a dar erro no valor das variáveis e da soma, desta maneira não há erros
int ra = pthread_create(&p, NULL, soma, (void *)(intptr_t)a);
int rb = pthread_create(&t, NULL, multiplicacao, (void *)(intptr_t)b);
pthread_join(t, (void **) &aux_multiplicacao);
pthread_join(p, (void **) &aux_soma);
a = aux_soma;
b = aux_multiplicacao;
int soma_ab = a + b;
printf("\nIDthread soma = %d\n", (int) p);
printf("IDthread multiplicacao = %d\n", (int) t);
printf("a = %d\n", a);
printf("b = %d\n", b);
printf("Soma: a + b = %d\n", soma_ab);
exit(0);
}
Thanks in advance.