PDA

View Full Version : Mutex lock question



cjjoy1980
07-16-2009, 10:32 PM
Hi all,
I have a scenario where I need to use the mutex locks. The mutex locks are working fine, but sometimes I am getting into the dead lock situation.

Below is the summary of my code :

MUTEX LOCK
performTask();
MUTEX UNLOCK.

In some cases I get into the situation where the thread is hung at performTask() function and never returns. When this happens the other threads which are waiting are blocked.

Please let me how can I kill the thread which is hung at performTask() function. Apart from using timed Mutex, is there a way I can return from performTask() function after waiting for a specified time?.


THanks
Joy

bithub
07-16-2009, 11:09 PM
You should never kill a thread. There is usually a function to do so (pthread_kill for instance), but in my experience it will usually crash your program or cause some other undesired behavior.

The simple answer to your question is that you should not lock around functions that take a long time to finish. Take a look at your performTask() function, and ask yourself why you need a mutex lock around it. Since I have no idea what performTask() actually does, I can't really give you much advice here.

Elysia
07-17-2009, 02:45 AM
The real problem is probably that performTask is locking some other lock that a thread waiting is supposed to do, hence the deadlock. No, the real solution is to change your design to fix the problem in the first place.

brewbuck
07-17-2009, 10:05 AM
There are two possibilities, neither of which involves killing a process or using timed lock attempts:

1. The function which is holding the mutex has a bug, and is either not releasing the mutex, or is stuck in an infinite loop. Fix your bug.

2. The function which holds the mutex is naturally an extremely complex function, or one that legitimately takes a long time to finish. In this case, you should not be holding a contended mutex while performing this operation. Alter your design.