What happens when pthreads are waiting in mutex_lock / cond_wait?

I have a program that should get the most out of my processor.

It is multithreaded through pthreads, which do their job perfectly, except that they "only" get my kernels up to about 60% of the load, which, in my opinion, is not enough.

I'm looking for a reason and I ask myself (and thereby you) if the mutex_lock / cond_wait blocking functions are candidates?

What happens when a thread cannot work in such a function?

  • Does pthread switch to another thread that it processes, or
  • Does the thread pass its time to the system, and if so, can I change this behavior?

Hi,

Nobody

Additional information A parameter is one mainthread that fills the taskpool and the countless number of workers who select jobs from there and wait for a conditional signal that is signaled by broadcast when serialized calculation is performed. They continue with the values ​​from this calculation until they are completed, deliver mail and get the next job ...

+6
source share
2 answers

In a typical modern pthreads implementation, each thread is controlled by a kernel that is not like a separate process. Any blocking call like pthread_mutex_lock or pthread_cond_wait (but also, say, read ) will give time to the system. Then the system will find another suitable thread for planning, whether in the process or in another process, and start it.

If your program takes up only 60% of the CPU, it is more likely to block during I / O than when performing pthread operations, unless you have done something too complicated with your pthread operations.

+3
source

If a thread is expecting in mutex / condition, it does not use resources (well, it uses only a small amount). Whenever a thread enters a standby state, control switches to other threads. When the mutex is freed (or a condition variable is signaled), the thread wakes up and can acquire the mutex (if no other thread captures it first) and continue to work. If, however, some other thread receives a mutex (this can happen if several threads are waiting for it), the thread returns to sleep.

0
source

Source: https://habr.com/ru/post/892614/


All Articles