Thread: Unexpected behaviour of pthread_cond_broadcast

  1. #1
    Registered User
    Join Date
    Jan 2019
    Posts
    1

    Unexpected behaviour of pthread_cond_broadcast

    I wrote a small code sample that starts a number of counting and a number of waiting threads.
    The waiting threads are stopped `pthread_cond_wait` until they receive a signal. The signal is sent after the counting threads finish their tasks.


    The waiting threads receive their signal and each thread prints out its given, unique id.


    I would expect all waiting threads to receive the signal at the same time, so that each of them can proceed with the program. I noticed however, the outputs are not chaotic, in fact they even appear to be fairly ordered, like in FILO!


    There are now various places, where I could have gone wrong.


    Here is my code:


    Code:
        #include <pthread.h>
        #include <stdio.h>
        #include <stdlib.h>
        
        #define counting_threads 100
        #define waiting_threads 100
        
        int count = 0;
        int counting_thread_ids[counting_threads];
        int waiting_thread_ids[waiting_threads];
        pthread_mutex_t count_mutex;
        pthread_cond_t count_threshold_cv;
        
        
        void init_ids(){
        	for(int i = 0; i < counting_threads; i++)
        		counting_thread_ids[i] = 2*i;
        	for(int j =0; j < waiting_threads; j++)
        		waiting_thread_ids[j] = 2*j+1; 	
        }
        
        void counting(void *t) 
        {
        	pthread_mutex_lock(&count_mutex);
        	count++;
        
            if (count == counting_threads) {
        	sleep(2);
              printf("inc_count(): count = %d  Threshold reached. Signaling waiting threads. \n", count);
                   //~ pthread_cond_signal(&count_threshold_cv);           
                   pthread_cond_broadcast(&count_threshold_cv); 
              }
            pthread_mutex_unlock(&count_mutex);
            }
        
        void *waiting(void *t) 
        {
          long my_id = (long)t;
          //~ printf("Starting watch_count(): thread %ld\n", my_id);
        
          pthread_mutex_lock(&count_mutex);
        
            //~ printf("watch_count(): I start waiting now: %ld \n", my_id);
            pthread_cond_wait(&count_threshold_cv, &count_mutex);
            printf("watch_count(): thread %ld Condition signal received.\n", my_id);
        	pthread_mutex_unlock(&count_mutex);
        	pthread_exit(NULL);
        }
        
        int main (int argc, char *argv[])
        {
        	init_ids(); 
        	
        	pthread_t wt[waiting_threads];
        	pthread_t ct[counting_threads];  
        	
          /* Initialize mutex and condition variable objects */
          pthread_mutex_init(&count_mutex, NULL);
          pthread_cond_init (&count_threshold_cv, NULL);
        
        	for(int i = 0; i < waiting_threads; i++)
        		pthread_create(&wt[i], NULL, waiting, (void*) waiting_thread_ids[i] );
        	for(int i = 0; i < counting_threads; i++)
        		pthread_create(&ct[i], NULL, counting, (void*) counting_thread_ids[i] );
        	
        	
          /* Wait for all threads to complete */
          for (int i=0; i<waiting_threads; i++) {
            pthread_join(wt[i], NULL);
          }
          for (int i=0; i<counting_threads; i++) {
            pthread_join(ct[i], NULL);
          }
        
        
          /* Clean up and exit */
          pthread_mutex_destroy(&count_mutex);
          pthread_cond_destroy(&count_threshold_cv);
          pthread_exit(NULL);
        
        }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,712
    > I would expect all waiting threads to receive the signal at the same time, so that each of them can proceed with the program.
    > I noticed however, the outputs are not chaotic, in fact they even appear to be fairly ordered, like in FILO!
    pthread_cond_signal
    If more than one thread is blocked on a condition variable, the scheduling policy determines the order in which threads are unblocked.
    That alone suggests there is an "order" as opposed to some kind of free-for-all "chaos".



    See also pthread_attr_setschedpolicy
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. unexpected output
    By alien1 in forum C Programming
    Replies: 2
    Last Post: 11-07-2014, 02:24 PM
  2. Beginner C query: unexpected behaviour in programme
    By Jimbo1981 in forum C Programming
    Replies: 5
    Last Post: 01-15-2011, 09:39 AM
  3. Unexpected variable behaviour
    By Dondrei in forum C++ Programming
    Replies: 5
    Last Post: 02-24-2009, 12:11 AM
  4. Unexpected behaviour with float
    By j.sreejit in forum C Programming
    Replies: 6
    Last Post: 09-14-2006, 09:53 PM
  5. Unexpected behaviour
    By fnoyan in forum C++ Programming
    Replies: 5
    Last Post: 03-05-2005, 09:45 AM

Tags for this Thread