CSCB09 2026 Summer Lab Week 12

Thread Safety

In this lab, we practice using a mutex and a condition variable to make a data structure thread-safe—free of race conditions when multiple threads operate on the same data structure.

queue.c is a usual implementation of FIFO queues by a linked list. We need a mutex so multiple threads operating on the same queue is safe. We also need a condition variable because one function intends to wait until the queue is non-empty. Here is an outline of your job:

Sample Tester

queue_test.c is a sample tester.

Without your addition for thread safety, it is actually so bad that more than 99% of the time it hangs in the middle!

After your addition, it is true that the order of data that the reader sees is non-deterministic. We won’t worry about it—at least it receives all data successfully!

Debugging And Error Messages

If you like to print debugging or error messages for your own sake, please send them to stderr only.

Handing In

Please hand in the amended queue.c.