Home Demos Forum Research Guide
Viewing topic in forum 'general'
back to topic list

deadlock situation
how can I create a Deadlock situation that would require the use of a mutex.
by adetolaraphael on 2015-05-29 14:13:36 (1 reply)

Hi,

I'm not quite sure I know what you're asking, but mutex blocks are used to avoid things like "lost updates" that occur when two threads may access and modify a shared piece of state. By using a mutex you can make sure that only one thread at a time can do this. If you're familiar with Java, the Java "synchronised" blocks perform a very similar role.

Imagine you have a function:


int counter = 0;

void MyInterface:incrementCounter()
   {
   counter = counter + 1;
   }
 


And you have multiple concurrently running threads that might call incrementCounter(). If we don't have any protection on this logic it's possible that two or more threads will simultaneously try to add 1 to the counter variable, creating an undefined result. So here you'd want to define a mutex around the logic inside incrementCounter().

If your question relates to the possibility of causing a deadlock with mutexes, this can happen if one thread acquires access to a mutex and then never releases it while another thread needs access to that same mutex.

A simple example would be:


Mutex lockA;
Mutex lockB;

void functionA()
   {
   mutex(lockA)
      {
      mutex(lockB)
         {
         }
      }
   }

void functionB()
   {
   mutex(lockB)
      {
      mutex(lockA)
         {
         }
      }
   }
 


Here both functions try to acquire both locks in opposite orders which could create a mutual deadlock. There are lots of other possible ways to cause deadlocks though.

I hope that helped a little - if you can expand on your question we can potentially provide a more specific answer.

Best regards,

- Barry
by barry on 2015-05-29 20:42:16
© Francis Research Ltd. 2017