Practice ยท 1 of 1
The Bounded Queue
Implement the bounded queue. The boilerplate declares:
``c
#define QCAP 4
typedef struct {
long items[QCAP];
size_t head, count;
mtx_t m;
cnd_t not_full, not_empty;
} BQ;
void bq_init(BQ *q); /* inits mutex + cnds */
void bq_destroy(BQ *q);
/* push: blocks while full; 0 ok */
int bq_push(BQ *q, long v);
/* pop: blocks while empty; 0 ok */
int bq_pop(BQ *q, long *out);
size_t bq_count(BQ *q); /* call only when no workers run */
``
Producers/consumers will run against it in tests โ the predicate
re-check MUST be a while loop.
Difficulty: intermediate
Press Submit to check your solution.
Back to lesson: Practice: Producer/Consumer Gym