Thread Per-Request (Cont.) The function that dequeues requests (runs in the manager thread): void* handle_requests(void* data) { struct mgr_thread_data* pdata = (struct mgr_thread_data*)data; struct request* r; /* lock the requests mutex. */ pthread_mutex_lock(&pdata->q->mutex); while (1) { /* check if we're asked to exit. */ if (pdata->should_exit) break; /* wait if the queue is empty. */ if (q_size_nolock(q) == 0) { pthread_cond_wait(&pdata->q->cond, &pdata->q->mutex); } /* handle request, if any pending. */ if (r = q_pop_nolock(q)) { pthread_mutex_unlock(&pdata->q->mutex); launch_request_handler(pdata, r); /* see below */ pthread_mutex_lock(&pdata->q->mutex); } }