Need help about lock hierarchy design
- From: "Kürşat" <xx@xxxxxx>
- Date: Wed, 4 Jun 2008 11:33:41 +0300
Hi,
I will give you an example to try to explain what I need :
Suppose we have two types of objects : Parent and Child.
The parent may have numerous childs as expected. Both parent and child
objects have it's own synchronization object, that is, a child can lock
itself without locking the parent. But if locks to be nested then a lock
hierarchy should be followed to prevent deadlocks and it may naturally be
parent-first. Now let's look such a lock hierarchy :
lock (); // We are in the parent object's scope...
m_pChild->lock();
if (child->hasData ())
{
DWORD dwChildDataSize = m_pChild->getDataSize();
memcpy (m_pData + m_dwOffset, m_pChild->getData(),
dwChildDataSize); // Transfer data to parent...
m_dwOffset += dwChildDataSize;
m_pChild->discardData();
}
m_pChild->unlock ();
unlock (); // Unlock parent...
I think so far so good but unconditionally locking the parent can cause
unnecessary contention because if the child does not have any data to
transfer then it will not be modify the parent. But how can we know if a
client has data to transfer in advance? We can query the client about data
existence and conditionally lock the parent but this can not guarantee
atomicity because we should first lock the child, check if data exists,
unlock the child for lock hierarchy not to break, then conditionally lock
the parent and lock child again. We may be caught by a context switch
between unlocking the child and loking the parent then our test becomes
invalid.
I need the best design to use in such a stuation, what is your idea?
Thanks in advance
.
- Prev by Date: Re: Get Handle Owner,...
- Next by Date: Re: Volume Serial No
- Previous by thread: Get Handle Owner,...
- Next by thread: Using Setup API to determine if a drive is on USB
- Index(es):
Relevant Pages
|