Project 21: Thread Pool Implementation

Project 21: Thread Pool Implementation

Build a reusable thread pool library with a bounded work queue, condition variables, and clean shutdown semantics.

Quick Reference

Attribute Value
Difficulty Advanced
Time Estimate 1-2 weeks
Language C (Alternatives: Rust, Go, Java)
Prerequisites pthread basics, mutexes/condvars, producer-consumer pattern
Key Topics Work queues, condvars, shutdown, backpressure, avoiding thundering herd
CS:APP Chapters 12

1. Learning Objectives

By completing this project, you will:

  1. Implement a correct producer-consumer queue with blocking enqueue/dequeue
  2. Build a fixed-size worker pool that executes user-provided tasks
  3. Handle shutdown safely (no task loss surprises; no deadlocks)
  4. Integrate your pool into a network server (P20) or proxy (P17)

2. Project Specification

2.1 API Surface (Suggested)

  • threadpool_create(nthreads, queue_capacity)
  • threadpool_submit(pool, fn, arg) (blocks or fails when full)
  • threadpool_wait(pool) (waits for queue drain + workers idle)
  • threadpool_destroy(pool) (graceful shutdown)

2.2 Example Usage

threadpool_t *pool = threadpool_create(8, 1024);
threadpool_submit(pool, handle_client, (void *)(intptr_t)connfd);
threadpool_destroy(pool);

3. Design Notes

  • Decide up front: does submit() block when full, or return an error?
  • Track โ€œin flightโ€ tasks separately from queue length to implement wait().
  • Use pthread_cond_signal vs pthread_cond_broadcast intentionally.

4. Testing Strategy

  • Unit tests for queue correctness under contention.
  • Stress tests with many producers and a few workers, then reverse.
  • Death tests: destroy while tasks still queued; ensure clean behavior.

5. Extensions

  • Per-worker local queues + work stealing.
  • Task priorities and deadline scheduling.
  • Metrics: queue depth, worker utilization, p50/p99 task latency.

6. Reference Reading

  • CS:APP 3e โ€” Ch. 12 (Concurrent Programming)