0
votes
void * worker(void * arg){
    threadData * p = (threadData *)arg; // Has thread info like what spots in the array this thread should change
    rowHead * row_head = p->info; // Has what rows need to mutiplied and which ones subtracted from
    pthread_cond_wait(&no_task_cond, &no_task_mutex); // Wait for new data to be added.
    pthread_mutex_unlock(&no_task_mutex); // Instantly give up lock since there is no critical data to protect
    pthread_cond_broadcast(&no_task_cond);
    // Go through the tasks and complete them
    rowNode * row_info = row_head->head;
    while(row_info != NULL){ // If we have a task then execute
        thread_multiplication(p->rank, p->t_row, row_info->row, row_info->multi, row_info->sub_row, p->dim, *p->matrix);
        row_info = row_info->next;
    }
    return 0;
}

What I would like to happen is for a group of threads to come into worker. Wait for all the tasks to be added to the linked list row_info and then once the main thread calls p_thread_broadcast(&no_task_mutex) for all of the threads to wake up, go through the linked list and do their tasks before exiting the function.

I'm not certain but I'm pretty sure it's getting stuck on the wait condition at the beginning which makes no sense to me since it should always call p_thread_broadcast at least once. It might have to do with my wait and broadcasts as they don't seem to work how I thought they should. When I have a cond_wait and call a broadcast that should wake up all the threads and if they immediately give up the lock they should all go at the same time right? Especially if I have the broadcast afterwards which I added to be safe

1

1 Answers

1
votes

The pthread_cond_timedwait() and pthread_cond_wait() functions shall block on a condition variable. The application shall ensure that these functions are called with mutex locked by the calling thread; otherwise, an error (for PTHREAD_MUTEX_ERRORCHECK and robust mutexes) or undefined behavior (for other mutexes) results.

Now... where did you lock the mutex prior to calling pthread_cond_wait?