"Joseph M. Newcomer" wrote:
See below...
On Fri, 25 May 2007 00:01:01 -0700, Alamelu <Alamelu@discussions.microsoft.com> wrote:
Class1::Method1( )
{
CSingleLock crsLock(&m_CSToken, true); // Where m_CSToken is CCriticalSection
.......
}
Class1::Method2( )
{
CSingleLock crsLock(&m_CSToken, true);
........
}
1) First Method1 is getting executed and able to gain the lock (From Thread1).
2) Method2 is trying to gain lock and fails to gain and it is blocking
Thread2 (From Thread2)
3) Method1() is posting sync event to Thread2 and in step 2, Thread2 is
already blocked
This make the application to hang.
*****
Yes. Fix you code so this does not occur. This is called "deadlock" and it is the result
of incorrect program structure. You simply cannot do synchronization like this, where one
thread blocks another. It is considered an incorrect program.
What do you mean "posting sync event"? PostMessage? What is a 'sync event'?
There may be a way to restructure the code, but right now, your program will require a
rewrite to make sure that there is no way that a thread that requires interaction with
another thread will block on that other thread.
Rule of synchronization: block the smallest amount of code for the shortest possible time.
This means that if you are blocking some long computation that has interfactions with
another thread, you have violated this rule and you will have to redesign your strategy.
joe
*****
Can somebody tell how to resolve this?
Regards,
Alamelu N
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm