Re: Avoiding Deadlocks using Template Metaprogramming
On 10 Dec., 16:20, n.j.wh...@gmail.com wrote:
Hi,
I've been experimenting with using template metaprogramming to enforce
deadlock-free code. The code below enforces an order that the locks
must be acquired in, and enforces that the locks are released in the
reverse order that they are acquired. This is not two-phase locking,
but should guarantee that no deadlocks occur.
It's not quite complete yet, and there is a lot of room for
improvement - there's a list of points at the bottom I'd appreciate
your thoughts on...
[snip]
Sorry for not commenting on your code; I just have a feeling that you
are trying to solve a rather simple problem in a quite complicated
way.
To avoid deadlocks, the only requirement that must be fullfilled is
that there is a specific locking order - so that one process will not
try lock(a), lock(b) whereafter another process tries to do lock(b),
lock(a). This can be accomplished ratger easily if you simply
associate an integer with every mutex (not lock!), keep hold of your
current most "highest" mutex and verify that any locking of a new
mutex satisfies that current_value < new_value.
In the programs, I've made, I've simply defined the mutex-values
manually and kept the mutexes acquired in a singly-linked list, but
you could also dynamically define the ordering in a graph.
So I do not believe you need all the machinery, I so carelessly
snipped.
/Peter
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]