-1

I would like to know if pthreads is doing busy waiting internally when calling pthread_cond_timedwait()?

I'm analysing a multi-threaded program and need to know if a thread is potentially blocking ressources when calling the above function.

Platform: Intel x86_64, CentOS 7.5.1804 with GCC 5.3.0

new2f7
  • 51
  • 6
  • 1
    Short answer: "no." (You can easily detect busy-waiting because the CPU utilization will be at or near 100% when the program isn't "doing" anything.) If the timeout interval passes without the condition being signaled, the call will end with an appropriate error-code. But the system is not "busy waiting" in the meantime. The bug in your application that you're looking for lies somewhere else ... – Mike Robinson Feb 10 '20 at 18:57
  • It only blocks the calling thread – Eugene Sh. Feb 10 '20 at 18:58
  • `Platform:` - what operating system? What implementation of `pthread_cond_timedwait()` are you using? [Looks like](https://github.com/lattera/glibc/blob/master/nptl/pthread_cond_wait.c#L502) glibc uses futexes. – KamilCuk Feb 10 '20 at 19:11
  • I updated the question with a more precise platform specification. I also clarified my intention. My program is always "doing something", there are more threads than execution units and I need to make sure that the calling thread is not obstructing the workers. How do I identify which implementation I use? Does futexes mean busy waiting or no busy waiting? – new2f7 Feb 10 '20 at 22:12

1 Answers1

2

As you will have read in their documentation, the pthread_cond_wait() and pthread_cond_timedwait() functions cause the calling thread to block until the CV is signaled, or, in the latter case, the specified time arrives. (Or the thread is cancelled, or it is temporarily aroused to handle a signal, or ....) Blocking does not merely mean that the thread does not return from the function. It means that the thread is not scheduled on any execution unit as long as it remains blocked. Thus, no, threads blocked in pthread_cond_timedwait() do not busy-wait.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157