Thread Per-Request (Cont.) /* launch queue manager thread. */ pthread_create(&mgr_thread, NULL, handle_requests, mgr_data); generate_requests(); wait_for_no_new_requests(); /* ask queue manager thread to exit. */ pthread_mutex_lock(mgr_data->q->mutex); *(mgr_data->should_exit) = 1; pthread_cond_signal(mgr_data->q->cond); pthread_mutex_unlock(mgr_data->q->mutex); /* and wait for manager thread to terminate. */ pthread_join(&mgr_thread, &thr_retval); return 0; }