Note that there are some explanatory texts on larger screens.

plurals
  1. POpthread_mutex_timedlock and the deadlock
    primarykey
    data
    text
    <p>Typically if a task1 holds lock A wants to take a lock B and another task2 has taken lock B and is waiting for lock A held by task1), this causes the deadlock.</p> <p>But when it comes to pthread_mutex_timedlock, it attempts the mutex lock or timeout after the specified timeout.</p> <p>I hit the deadlock scenario where i was trying to take the timed lock, which would have timed out eventually, which puzzles me.</p> <p><strong>edit: Deadlocks can be avoided by having a better design, which is what i ended up doing, i made sure that the order of taking mutex locks is same, to avoid deadlock but the question remains open as to if the deadlock can be avoided since i chose timedlock</strong> </p> <p>Can someone explain me this behaviour ?</p> <p><strong>Edit: Attaching a sample code to make the scenario more clear(real tasks are fairly complicated and run into thousands of lines)</strong></p> <p>T1 </p> <pre><code>pthread_mutex_lock(&amp;lockA); //call some API, which results in a lock of m2 pthread_mutex_lock(&amp;lockB); //unlock in the order pthread_mutex_unlock(&amp;lockB); pthread_mutex_unlock(&amp;lockA); </code></pre> <p>T2</p> <pre><code>pthread_mutex_lock(&amp;lockB); //call some API, which results in locking m1 pthread_mutex_timedlock(&amp;lockA,&lt;10 sec&gt;); </code></pre> <p>The crash is seen in the context of T2, bt:</p> <pre><code>Program terminated with signal 6, Aborted. #0 0x57edada0 in raise () from /lib/libc.so.6 (gdb) bt #0 0x57edada0 in raise () from /lib/libc.so.6 #1 0x57edc307 in abort () from /lib/libc.so.6 #2 0x57ed4421 in __assert_fail () from /lib/libc.so.6 #3 0x57bb2a7c in pthread_mutex_timedlock () from /lib/libpthread.so.0 </code></pre> <p>I traced the error to following </p> <pre><code>pthread_mutex_timedlock: Assertion `(-(e)) != 35 || (kind != PTHREAD_MUTEX_ERRORCHECK_NP &amp;&amp; kind != PTHREAD_MUTEX_RECURSIVE_NP)' failed. </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload