I am asked following question in an Interview:
1) There are two threads: T1 and T2. They are sharing one resources and to avoid deadlock using pthread_mutex for synchronizing. How you will design your code such that if any segmentation fault happen after T1 enters critical section, T2 will not be in deadlock?
//T1 Code
try
{
pthread_mutex_lock(somelock);
....
/// work on shared memory
//What will happen if segfault happens here?
....
pthread_mutex_unlock(somelock);
} catch(...)
{
pthread_mutex_unlock(somelock);
// exception happens
}
I told I dont know the ans. Interviewer reached this situation waiting for my ans.
Is there really any design to avoid deadlock in this situation?
Above codeblock just for understanding. I read this. But its not clear Thanks in Advance.