Thread Per-Request (Cont.) The function which launches a new handler thread for a request - including handling of "flow control". void launch_request_handler(struct mgr_thread_data* pdata, struct request* r) { struct handler_thread_data* p_ht_data; pthread_t thread; /* first, make sure there are not too many */ /* active handler threads. */ pthread_mutex_lock(&pdata->threads->mutex); while (pdata->threads->num_threads >= pdata->threads->max_num_threads) { pthread_cond_wait(&pdata->threads->cond, &pdata->threads->mutex); } /* we're creating one more handler thread. */ pdata->thread->num_threads++; pthread_mutex_unlock(&pdata->threads->mutex);