0

I have an assignment to complete and got stucked at one point . Is the classical producer-consumers problem with 1 producer, n multiple consumers and a MAX number of items produced.

I have to use Condition Variables. I have 2 big doubts:

1- How to stop the consumers when all the items are created by the producer? I mustn't use global variables to solve this.

My problem is that all the consumers remain waiting for the producer that has just finished its execution because he produced all the items.

2-Each item has a random id that represents the id of the consumer that is supposed to consume it. So a consumer can only consume its own item (marked by the producer randomly at the beginning). How to implement this with condition variables.

My code works with 1 prod and 1 cons. When I have more than 1 consumer I don't know how to terminate the consumers thread when all the items are produced.

This is my code now:

static void * producer (void * arg)
{
   ITEM    item;

   int j = 1;
   while (j <=  NROF_ITEMS)
   {
       rsleep (PRODUCER_SLEEP_FACTOR);


          item = create_item(j);

           pthread_mutex_lock (&mutex);
           while(item_counter == BUFFER_SIZE)
              pthread_cond_wait(&not_full_condition, &mutex);

        put(item);
        printf("%04x\n", item); // write info to stdout

        pthread_cond_signal (&not_empty_condition);

        pthread_mutex_unlock (&mutex);
        j++;

    }


    pthread_exit(0);

   // TODO: 
   // * inform consumers that we're ready

    //HERE I NEED TO STOP THE CONSUMER THREAD
}


static void * consumer (void * arg)
{
    ITEM    item;   // a consumed item
    int     id;

    int * argid;
    argid = (int*) arg;
    id = *argid;
    free(arg);


     while (i < NROF_ITEMS)
    {
        rsleep (100 * NROF_CONSUMERS);

         pthread_mutex_lock (&mutex);


              while(item_counter == 0)        
                pthread_cond_wait(&not_empty_condition, &mutex);

              printf("\n C%d signalled by the producer",id);

              item = get_after_check();
              printf("%*s    C%d:%04x\n", 7*id, "", id, item);

              pthread_cond_signal (&not_full_condition);




            pthread_mutex_unlock (&mutex);
            i++;         

 }

   pthread_exit(0);
}
kensei
  • 13
  • 1
  • 3
  • (1) Have a flag in function where you are creating producer and consumer threads and pass the pointer to this flag to producer and all consumer thread as thread function argument. Always check this flag before waiting in consumers. If flag is enabled exit consumer thread function. In producer after producing all items enable this flag and call pthread_cond_broadcast. This should solve your problem. Please note, you have to make sure all produced items are consumed by adding appropriate logic. – MayurK Oct 23 '16 at 17:43
  • (2) There is a similar question. Check if it is useful: http://stackoverflow.com/questions/40205329/producer-consumers-multithreading-cant-terminate-program – MayurK Oct 23 '16 at 17:43
  • for the (1) how can I be sure that all produced items are consumed without using a global variable? And how can I pass the flag to the consumers if i am passing to it his id as a argument of the function? – kensei Oct 23 '16 at 18:03
  • (1): What is "item_counter" in your code? – MayurK Oct 23 '16 at 18:06
  • is the counter that i increment or decrement when produce or consume. I use it to check if the bounded buffer is empty or full and so I use it in the whie loop for condition variables – kensei Oct 23 '16 at 18:14
  • That is what you want right? If your counter is 0, it means all produced items have been consumed. Isn't it? – MayurK Oct 23 '16 at 18:25
  • it's not the counter for count how many items are produced but to count how many items are in the buffer. This is my mistake i should call it properly – kensei Oct 23 '16 at 18:45
  • I declared a global variable `bool flag` and after all the items are produced by the producer I set as true. In the consumers I put an: `if(flag == true) break;` It's not working – kensei Oct 23 '16 at 19:58
  • I understand that "item_counter" is number of items in buffer. If the counter is zero, it means all items are consumed right? About the flag.. You said using global variables is not allowed. Anyways, if it is working and the design is OK for you we can close this discussion. – MayurK Oct 25 '16 at 04:42

0 Answers0