2

This answer claims there is no guarantee that pthread_mutex_lock ever returns, even if no deadlock occurs, and likewise there is no guarantee that pthread_cond_wait ever returns, even if the condition variable is signalled by another thread. Worse, this answer claims that the chance that these functions never return is realistic and not purely theoretical.

Now I’m sorry to post a question that questions an answer I was already given. But tbh I find this pretty strange. If this is the state of affairs, then this whole interface would seem pretty much unusable for me, as there seems to be no way for the programmer to prevent the application from freezing, even if the code is free of bugs!

Can it really happen that phread_mutex_lock never returns even if no deadlock occurs? Can it really happen that pthread_cond_wait never returns even if the condition variable is signalled? If so, how to prevent the app from sudden freezes?

  • In that answer you have been given there is a reference to [another one](https://stackoverflow.com/a/44417229/2989411). Have you seen it? Have you played with the examples (especially first one)? What was your results? Now, what would happened if instead of `for` loop it would be `while(1)` one? Also, you have been given a solution in the same example. – ArturFH Jun 09 '17 at 08:23
  • 1
    The problem is known as *starvation* a situation where a thread is unable to have access to a resource and make some progress. To realize fairness is a difficult task; most of the time it is said that responsibility is in the scheduler. This may give you hints: https://stackoverflow.com/questions/15910759/how-to-prevent-threads-from-starvation-in-c11 – Jean-Baptiste Yunès Jun 09 '17 at 08:49
  • Or this one https://stackoverflow.com/questions/20261592/threads-access-resource-avoiding-starvation – Jean-Baptiste Yunès Jun 09 '17 at 08:50
  • Realistically, why do you care? If you need to acquire the mutex and can't, there's nothing you can do. And if you decide to spend the time and try finding some hypothetical problem with the mutex implementation on your platform, what are you going to do if you do find one? Spend the time fixing it and getting the fix accepted? What if your system isn't properly designed and is prone to [halt and catch fire](https://en.wikipedia.org/wiki/Halt_and_Catch_Fire)? Do you worry about that when you don't have actual evidence your system is prone to do that? – Andrew Henle Jun 09 '17 at 10:54

0 Answers0