What does having sched_yield
as a major hotspot, mean for my program?
On Linux, sched_yield
does not necessarily switch to another thread to execute. The kernel does not deschedule the calling thread if there aren't threads that are ready to run on the same CPU. The last part is important, since the kernel will not reschedule a ready to run thread between CPUs upon this call. This is a design tradeoff, as sched_yield
is supposed to be a low cost hint to the kernel.
Since sched_yield
may just return immediately without doing anything, your code may act as having a busy loop around this call, which will look like a hot spot in your profile. Your code just loops around sched_yield
a lot, without doing much else. Such spinning burns a lot of CPU cycles which could be spent for other threads and applications running on the system.
What would be a remedy for this situation?
This depends a lot of your use case. Using sched_yield
may be acceptable when you are willing to waste some CPU cycles in exchange for better latency. You have to be conscious about this decision, and even then I would recommend benchmarking a different solution, with proper thread blocking. Linux thread scheduler is quite efficient, so blocking and waking threads is not as expensive as on some other systems.
Often sched_yield
is used in custom spin lock algorithms. I would recommend replacing these with pthread components, in particular pthread_cond_t
, which allow to properly block and wake up threads. If you're using C++, there are equivalents in the standard library (e.g. std::condition_variable
). In other cases it may be worth exploring other blocking APIs, such as select
and epoll. The exact solution depends on your use case.