I am learning parallel programming using Pthreads. I wrote the following simple program as a try. It takes two vectors from stdin (the user specifies the length as command line argument) then it adds them, subtracts them, multiply them element-wise and divide them element-wise by creating a thread for each operation of the four. the problem is sometimes the code works fine and when i use the same input again it just prints zeros.
Why does it behave like this?
I am using Ubuntu 14.04 on virtual machine.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define loop(i, n) for (i = 0; i < n; i++)
int thread_count, n;
int *a;
int *b;
int** results; // holds the four output vectors
void* math(void* opl);
int main(int argc, char* argv[]){
int thread = 0, i;
pthread_t* thread_handles;
n = strtol(argv[1], NULL, 10);
results = malloc(4 * sizeof(int*));
a = malloc(n * sizeof(int));
b = malloc(n * sizeof(int));
loop(i, n)
scanf("%d", a + i);
loop(i, n)
scanf("%d", b + i);
thread_handles = malloc(4*sizeof(pthread_t));
loop(thread, 4){
results[thread] = malloc(n * sizeof(int));
pthread_create(&thread_handles[thread], NULL, math, (void *)thread);
}
printf("Hello from the main thread\n");
loop(thread, 4);
pthread_join(thread_handles[thread], NULL);
loop(thread, 4){
printf("Results of operation %d:\n", thread);
loop(i, n)
printf("%d ", results[thread][i]);
printf("\n");
free(results[thread]);
}
free(thread_handles);
free(a);
free(b);
free(results);
return 0;
}
void* math(void* op1){
int op = (int) op1, i;
printf("%d ", op);
if (op == 0)
loop(i, n)
results[op][i] = a[i] + b[i];
else if (op == 1)
loop(i, n)
results[op][i] = a[i] - b[i];
else if (op == 2)
loop(i, n)
results[op][i] = a[i] * b[i];
else
loop(i, n)
results[op][i] = a[i] / b[i];
return NULL;
}