0
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

dmb1o3
  • 21
  • 2

1 Answers1

1

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?

  • I never do. I just kind of assumed that it locked the mutex when it was signaled. If I lock the mutex right before I wait and then give up the lock would it run properly? – dmb1o3 Oct 11 '20 at 18:29
  • well, the question is how does your main thread know that *any* of the threads is waiting for the condition yet... – Antti Haapala -- Слава Україні Oct 11 '20 at 18:29
  • @dmb1o3: A condition variable should never be used without a [predicate](https://stackoverflow.com/questions/22837254/predicate-for-condition-variable). All access to the predicate should be protected by the mutex. Are you using a predicate or are you using only the condition variable itself for inter-thread communication? – Andreas Wenzel Oct 11 '20 at 18:34
  • @AndreasWenzel I am using only the condition variable itself. I'll give the link you sent a read. – dmb1o3 Oct 11 '20 at 19:19
  • @dmb1o3: The link I gave you was just to explain the word "predicate". I think you need to learn more about the purpose and usage of condition variables. I belileve Wikipedia has [a good article](https://en.wikipedia.org/wiki/Monitor_(synchronization)) on this. However, you might be able to find a better tutorial using Google. – Andreas Wenzel Oct 11 '20 at 19:28
  • @dmb1o3: This question may also be of interest to you: [Why do pthreads’ condition variable functions require a mutex?](https://stackoverflow.com/q/2763714/12149471) – Andreas Wenzel Oct 11 '20 at 20:41