Why do you need conditional variables when you have mutexes?

112 views Asked by At

I've taken a look at a Wikipedia pseudo code showing the consumer-producer problem solution using semaphores and mutexes:

mutex buffer_mutex; 
semaphore fillCount = 0;
semaphore emptyCount = BUFFER_SIZE;

procedure producer() {
    while (true) {
        item = produceItem();
        down(emptyCount);
            down(buffer_mutex);
                putItemIntoBuffer(item);
            up(buffer_mutex);
        up(fillCount);
    }
}

procedure consumer() {
    while (true) {
        down(fillCount);
            down(buffer_mutex);
                item = removeItemFromBuffer();
            up(buffer_mutex);
        up(emptyCount);
        consumeItem(item);
    }
}

This solution seems to me like it would work pretty well, But how exactly will condition variables help us here? From what I understood a CV will block the calling thread until a certain condition is met, But the 'down' operation or a 'lock' operation also blocks the calling thread if the the value is 0. So is it all about integers vs conditions or is there more to it? Thanks.

0

There are 0 answers