Implementing A Thread Pool (Cont.) while (1) { /* check if we're asked to exit. */ if (pdata->should_exit || *(pdata->pool_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); handle_request(r); pthread_mutex_lock(&pdata->q->mutex); } } /* unlock requests mutex. */ pthread_mutex_unlock(&pdata->q->mutex); return NULL; }