Skip to main content
Engineering LibreTexts

10.2: Producers and consumers

  • Page ID
    40635
  • Now let’s make some threads to access this queue. Here’s the producer code:

    void *producer_entry(void *arg) {
        Shared *shared = (Shared *) arg;
    
        for (int i=0; i<QUEUE_LENGTH-1; i++) {
            printf("adding item %d\n", i);
            queue_push(shared->queue, i);
        }
        pthread_exit(NULL);
    }
    

    Here’s the consumer code:

    void *consumer_entry(void *arg) {
        int item;
        Shared *shared = (Shared *) arg;
    
        for (int i=0; i<QUEUE_LENGTH-1; i++) {
            item = queue_pop(shared->queue);
            printf("consuming item %d\n", item);
        }
        pthread_exit(NULL);
    }
    

    Here’s the parent code that starts the threads and waits for them

        pthread_t child[NUM_CHILDREN];
    
        Shared *shared = make_shared();
    
        child[0] = make_thread(producer_entry, shared);
        child[1] = make_thread(consumer_entry, shared);
    
        for (int i=0; i<NUM_CHILDREN; i++) {
            join_thread(child[i]);
        }
    

    And finally here’s the shared structure that contains the queue:

    typedef struct {
        Queue *queue;
    } Shared;
    
    Shared *make_shared()
    {
        Shared *shared = check_malloc(sizeof(Shared));
        shared->queue = make_queue(QUEUE_LENGTH);
        return shared;
    }
    

    The code we have so far is a good starting place, but it has several problems:

    • Access to the queue is not thread safe. Different threads could access array, next_in, and next_out at the same time and leave the queue in a broken, “inconsistent” state.
    • If the consumer is scheduled first, it finds the queue empty, print an error message, and exits. We would rather have the consumer block until the queue is not empty. Similarly, we would like the producer to block if the queue is full.

    In the next section, we solve the first problem with a Mutex. In the following section, we solve the second problem with condition variables.

    • Was this article helpful?