Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens when pthreads wait in mutex_lock/cond_wait?

I have a program that should get the maximum out of my cpu.

It is multithreaded via pthreads that do their job well apart from the fact that they "only" get my cores to about 60% load which is not enough in my opinion.

I am searching for the reason and am asking myself (and hereby you) if the blocking functions mutex_lock/cond_wait are candidates?

What happens when a thread cannot run on in such a function?

  • Does pthread switch to another thread it handles or
  • does the thread yield its time to the system and if the latter is the case, can I change this behavior?

Regards,

Nobody

More Information The setting is one mainthread that fills the taskpool and countless workers that fetch jobs from there and wait on a conditional that is signaled via broadcast when a serialized calculation is done. They go on with the values from this calculation until they are done, deliver their mail and fetch the next job...

like image 598
Nobody moving away from SE Avatar asked Jul 12 '11 21:07

Nobody moving away from SE


1 Answers

On a typical modern pthreads implementation, each thread is managed by the kernel not unlike a separate process. Any blocking call like pthread_mutex_lock or pthread_cond_wait (but also, say, read) will yield its time to the system. The system will then find another eligible thread to schedule, whether in your process or another process, and run it.

If your program is only taking 60% of the CPU, it is more likely blocked on I/O than on pthread operations, unless you have done something way too granular with your pthread operations.

like image 89
Nemo Avatar answered Sep 20 '22 08:09

Nemo