To solve a problem (and better my understanding of multitasking) I have written a small thread pool implementation. This thread pool spins up a number of worker threads which pop tasks off of a queue as they are added by the client of the thread pool. For the purposes of this question when the task queue is empty the worker threads are all terminated.
After doing some basic benchmarking I have discovered the application spends ~60% of its time waiting to acquire the queue lock. Presumably this is mostly taking place within the worker threads.
Is this merely an indication I'm not giving the worker threads enough to do, or something more? Is there something straightforward I may be missing to increase worker thread throughput?
EDIT: Here is some rough pseudocode that should illustrate things somewhat. These are the only two places where a lock is acquired/released during the execution of the worker threads (which is a vast majority of the running time of the application.)
std::list<task_t> task_list;
// Called by the client to add tasks to the thread pool
void insert_task(const task_t& task)
{
lock_type listlock(task_mutex);
task_list.push_back(task);
}
// The base routine of each thread in the pool. Some details
// such as lifetime management have been omitted for clarity.
void worker_thread_base()
{
while (true)
{
task_t task;
{
lock_type listlock(task_mutex);
if (task_list.empty())
continue;
task = task_list.front();
task_list.pop_front();
}
do_task(task);
}
}