In my code below, I create a pool of threads which do some computation on a common array. I want the computation to be done in parallel however, I want to print the results of the computation such that a thread id x doesn't print its results before thread id y, where x > y. So first I want to print the results of id 0, then 1, 2, etc.
I'm using pthreads to do this. Originally I used pthread_cond_broadcast
to wake up blocked threads (everything worked) but then I tried pthread_cond_signal
just out of curiosity. Interestingly enough, the program still works correctly.
But I don't understand why. That is:
pthread_cond_signal
"shall unblock at least one of the threads that are blocked". So maybe it unblocks thread id 3.So why does pthread_cond_signal
still work? Is it because of repeated sheer luck or because my OS creates a queue of blocked threads and thread id 1 just happens to be in the head of this queue?
This is my code (the wait/signal logic is in function ComputeThread
):
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#define DEFAULT_VAL 5
#define ARR_LEN 5
#define THREADS_NUM 5
typedef struct helper_t {
int * currId;
int threadId;
int * computeArr;
pthread_mutex_t * mutex;
pthread_cond_t * cond;
} helper_t;
int * initComputeArr(int len) {
int i;
int * computeArr = (int *) malloc(sizeof(int) * len);
for(i = 0; i < len; i++) {
computeArr[i] = DEFAULT_VAL;
}
return computeArr;
}
void mallocError() {
printf("malloc error\n");
exit(EXIT_FAILURE);
}
helper_t * initHelpers(pthread_mutex_t * mutex, pthread_cond_t * cond, int * computeArr) {
int i;
helper_t * helpers = (helper_t *) malloc(sizeof(helper_t) * THREADS_NUM);
int * currId = (int *) malloc(sizeof(int));
if(!helpers || !currId) {
mallocError();
} else {
*currId = 0;
for(i = 0; i < THREADS_NUM; i++) {
helpers[i].mutex = mutex;
helpers[i].cond = cond;
helpers[i].computeArr = computeArr;
helpers[i].currId = currId;
helpers[i].threadId = i;
}
}
return helpers;
}
void printHelper(helper_t * h) {
printf("threadId %d, currId %d\n", h->threadId, *h->currId);
}
void printHelpers(helper_t * h, int len) {
int i;
for(i = 0; i < len; i++) {
printHelper(&h[i]);
}
}
int calc(int * arr, int uptoIndex) {
int i, sum = 0;
for(i = 0; i <= uptoIndex; i++) {
sum += arr[i];
}
return sum;
}
void * ComputeThread(void * arg) {
int calcResult;
helper_t * h = (helper_t *) arg;
pthread_mutex_t * mutex = h->mutex;
pthread_cond_t * cond = h->cond;
calcResult = calc(h->computeArr, h->threadId);
sleep(1);
pthread_mutex_lock(mutex);
while(*h->currId != h->threadId) {
printf("id %d waiting...\n", h->threadId);
pthread_cond_wait(cond, mutex);
}
printf("curr %d, threadId %d, result %d\n", *h->currId, h->threadId, calcResult);
(*h->currId)++;
pthread_cond_signal(cond);
pthread_mutex_unlock(mutex);
pthread_exit((void *) calcResult);
}
int main() {
int i;
pthread_mutex_t mutex;
pthread_cond_t cond;
int * computeArr;
int * calcResutls;
helper_t * helpers;
pthread_t threads[THREADS_NUM];
computeArr = initComputeArr(ARR_LEN);
calcResutls = initComputeArr(ARR_LEN);
helpers = initHelpers(&mutex, &cond, computeArr);
printHelpers(helpers, THREADS_NUM);
for(i = 0; i < THREADS_NUM; i++) {
pthread_create(&threads[i], NULL, ComputeThread, (void *) &helpers[i]);
}
for(i = 0; i < THREADS_NUM; i++) {
pthread_join(threads[i], (void **) &calcResutls[i]);
}
for(i = 0; i < ARR_LEN; i++) {
printf("%d, ", calcResutls[i]);
}
printf("\n");
printf("end of calc\n");
return 0;
}
I see undefined behaviour getting invoked as the code misses to initialise the mutex and the condition.