Skip to content
Advertisement

why does pthread_cond_timedwait not trigger after indicated time-limit?

This is supposed to work in a loop (server) and delegate work/inquiry to a faulty library, here represented by the longrun() function call, to a thread with a time-out of tmax=3s. I placed synchronization vars and i am trying to wait for no more than this limit, but when longrun() hangs (run 4), it still waits the full time (7s) instead of the requested limit. Can anyone explain?

JavaScript

Used ‘g++ -pthread code.cc’ to compile under linux (ubuntu 16.04). Output is:

JavaScript

Advertisement

Answer

The problem is that faulty_proc() keeps tpx->mutex locked while it calls longrun(), and the pthread_cond_timedwait() call in main() can’t return until it can re-acquire the mutex, even if the timeout expires.

If longrun() doesn’t need the mutex to be locked – and that seems to be the case – you can unlock the mutex around that call and re-lock it before setting the completion flag and signalling the condition variable.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement