N
ndxp
I need to know if there are any thumb rules which i need to follow in
order to avoid deadlocks
in thread.
thanks
order to avoid deadlocks
in thread.
thanks
order to avoid deadlocks
in thread.
I need to know if there are any thumb rules which i need to follow in
order to avoid deadlocks
in thread.
I need to know if there are any thumb rules which i need to follow in
order to avoid deadlocks
in thread.
Know what you're doing.
Prove that your program cannot deadlock. (Note that a simple solution to
this is to ensure that a thread can only take out one lock at a time)
Alun Harford
I need to know if there are any thumb rules which i need to follow in
order to avoid deadlocks
in thread.
the main one is always acquire resources in the same order.I need to know if there are any thumb rules which i need to follow in
order to avoid deadlocks
in thread.
Proving a deadlock will not occur is quite hard for large complex
system.
I think we can probably minimize the possibility of a deadlock, but
cannot
completely avoid it. ( somebody tell me I'm wrong)
Roedy Green said:the main one is always acquire resources in the same order.
Stephen said:A more complete way of stating this is always acquire in the same order
and always release in the reverse order to acquisition. If you get the
releasing part wrong you can also have problems. Because of the way Java
exposes the releasing part of the problem this is not an issue with
Java, but can be with other languages (such as using the Win32 APIs
directly for say Delphi, C++ or writing Java bytecode directly as the
output for a given language).
Steve Horsley said:Can you explain why that might be? I can see that if a release call
resulted in a hidden release-acquire-release sequence that this could
be a problem - the hidden acquire might be out of order with other
acquisitions.
But I can't think why a clean release on its own might be problematic.
I need to know if there are any thumb rules which i need to follow in
order to avoid deadlocks
in thread.
I'm not sure you understood what I meant - you may have, just your
comment "clean release" makes me unsure, as I am not talking about a
clean release. Two examples:
This is OK. (this is what I'd call a clean release if using that
terminology).
1.acquire.
2.acquire.
3.acquire.
3.release
2.release
1.release
This is not OK.
1.acquire.
2.acquire.
3.acquire.
2.release <- mistake.
1.release <- mistake
3.release <- mistake
At some point another thread will be able to acquire 1 and 2 out of
order with the acquisition of 3. This will lead to problems, most likely
with some other thread that is using some or all of these locks.
I haven't got time to write you a code example that dies, but I've seen
some pretty nasty results in Win32 multi-threading as a result of things
like this.
Curious. Too bad you can't show us an example.
The examples are usually very convoluted and not easy to demonstrate (or
remember!) in a forum such as this. I'm not even sure I could write an
example on demand. Its part of my memory of implementation of multi
threaded systems and what caused them to fail.
This is OK. (this is what I'd call a clean release if using that
terminology).
1.acquire.
2.acquire.
3.acquire.
3.release
2.release
1.release
This is not OK.
1.acquire.
2.acquire.
3.acquire.
2.release <- mistake.
1.release <- mistake
3.release <- mistake
At some point another thread will be able to acquire 1 and 2 out of
order with the acquisition of 3. This will lead to problems, most likely
with some other thread that is using some or all of these locks.
Another issue not considered here is that releasing in the wrong order
also has the following potential liablilities:
o Possible performance drop as some locks will be held for longer than
needed.
o Possible data integrity problems as some locks will be released too
early.
Anyway, as I initially said, it isn't an issue in Java unless you are
writing bytecode directly.
If the other thread is performing the same sequence of acquisitions
(first 1, then 2, then 3), how is this a problem? The other thread
isn't going to attempt to acquire 2 until it has 1.
Not agreed on this one.
Maybe it would help if you could point me/us to an API for acquiring
and releasing resources that *does* have the kind of problems you're
talking about.
In that trivial example I posted it isn't a problem. As I said, the
problems I've come up against were convoluted examples a lot more
complex than several identical threads using the same locks. Most
software I've worked on is not multiple identical threads working with
the same locks. It is multiple different threads working with the same
locks plus their own locks, some of which may be used by other threads.
The trivial examples people use in these discussions (this one included)
do not represent the real world.
1.acquire
modify A
2.acquire
modify B
3.acquire
modify C
1.release
at this point another thread can modify A <- potential damage
3.release
2.release
The potential damage is because the A should be protected. Since you've
unlocked (1) in the wrong order A is not protected. 3 and 2 may still be
held for quite a long time. But 3 and 2 are not protecting A.
In a trivial example where the locks are acquired immediately one after
each other there is no problem. Real world examples usually have locks
acquired in different functions. Thus if you release one too early you
are exposing data that should be protected to potential manipulation
from another thread.
Win32 EnterCriticalSection/LeaveCriticalSection and the equivalent java
bytecode instructions for entering and leaving a monitor.
I don't understand your example here. You have three locks, numbered
1, 2, and 3?
Yes.
What does 1 represent?
from the fact that you acquire
it just before modifying A, it would seem to be something you need to
hold to modify A -- but then if the code modifies A after releasing
this lock, then .... I wouldn't call that a problem with releasing
locks in the wrong order so much as with failing to protect A by
only modifying it while holding the associated lock.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.