CSE Roadmap

Core tier · about 6 hours · 7 parts

Part 5 of 7 · 3 min read

Condition variables

In plain words: The same buffer can be built from a mutex and two condition variables (page 4): not_full for producers and not_empty for consumers. The item count is kept in a variable, and it settles the question of full against empty from section 2.

void put(int v) {
    pthread_mutex_lock(&mu);
    while (count == SLOTS) pthread_cond_wait(&not_full, &mu);   /* full: wait */
    buf[in] = v;  in = (in + 1) % SLOTS;  count++;
    pthread_cond_signal(&not_empty);                            /* an item is available */
    pthread_mutex_unlock(&mu);
}
int get(void) {
    pthread_mutex_lock(&mu);
    while (count == 0) pthread_cond_wait(&not_empty, &mu);      /* empty: wait */
    int v = buf[out];  out = (out + 1) % SLOTS;  count--;
    pthread_cond_signal(&not_full);                             /* a slot is free */
    pthread_mutex_unlock(&mu);
    return v;
}

Two rules from page 4 appear here: the wait is in a while loop, and each side signals the other condition variable. With one condition variable for both sides, a signal might wake a thread of the same kind, which cannot proceed, and the thread that could proceed would sleep on (the reason for using two).

Both versions were tested with the same producers and consumers. Each producer made 250,000 items with known values, and the sums of the values put and taken were compared. In every run, the sums matched, so no item was lost or duplicated. The throughput (items per second, a buffer of 8 slots):

Threads Semaphores Condition variables
1 producer, 1 consumer 216,000 to 268,000 268,000 to 306,000
2 and 2 560,000 to 608,000 261,000
4 and 4 752,000 to 773,000 187,000 to 189,000

Each figure range is two runs. The pattern: the semaphore version scaled with more threads in this test, and the condition-variable version did not. The cause was not examined here, so no conclusion about the tools in general should be drawn from it. A buffer of 8 slots is small, so threads sleep and wake very often, and the cost of waking dominates. Larger buffers and batching reduce it. The honest rule: measure in the real program.

The bounded size is also where backpressure comes from: when consumers are slower than producers, the buffer fills, the producers sleep, and the production rate falls to the consumption rate with no extra code.

Backpressure

Backpressure is the slowing of a producer by a full bounded buffer, which makes the producer wait until the consumer frees space, so that the rate of production adapts to the rate of consumption and memory use stays bounded.

Example: a file-reading thread that produces lines faster than the workers consume them sleeps in put when all 8 slots are full.

Check yourself: 1. Why does each side signal the other condition variable? 2. What did the matching sums show? 3. Why should the throughput table not be used to claim that semaphores are faster?