CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Feb 2004
    Location
    Germany
    Posts
    153

    Strange Deadlock while terminating a thread

    Hi,

    i encountered a strange error in my application.
    As the whole code is quite too complex to post here, i try a short version
    of the problem.

    First, I have a thread, which calls an Execute-Function in a Plugin-Kind
    class ( A user of our API is free to fill this Execute-Function with its own code ).
    In this case the Execute-Function consists of an endless while-loop, without any sleep in it, just allocating a huge amount of memory ( I know, not very
    sensfully, but the user is free to do this ).

    Now i want to stop this thread:
    I Send a Stop-Thread signal.
    If the Execute-Function is programmed properly it should react on this signal and leave the Execute.
    But in this case it does not.
    Now i wait for a, lets say 5 seconds, and then i Terminate the Thread.

    And now the obscure.
    When i try to delete the Thread-Object i run in a deadlock in the heap.
    ( O.K.: I forgot: i'm using MVS7.NET under WinXP prof. - standard C++ Dll Project ).
    I found, that is not a problem of the Debug-Heap ( Here i can see, where the thread hangs ). The problem also occurs in the release version.

    Any ideas about that ?
    Any hint is welcome.
    Greetings,
    Matze

  2. #2
    Join Date
    Feb 2002
    Posts
    5,757

    Re: Strange Deadlock while terminating a thread

    Describe the stop signal and the process of which the thread receives and analyzes that signal.

    Kuphryn

  3. #3
    Join Date
    Feb 2003
    Location
    BeiJing China
    Posts
    290

    Re: Strange Deadlock while terminating a thread

    Maybe the "execute function" has run over..
    Waiting OFFERS from CMU, UMass, UC Davis, OSU
    Mail to me

  4. #4
    Join Date
    Feb 2004
    Location
    Germany
    Posts
    153

    Re: Strange Deadlock while terminating a thread

    Hi kuphryn and wxuf,

    thank you for reply.
    I try to describe it with some (pseudo) code:

    Code:
    // The base class
    class CBaseClass
    {    
        virtual void Execute() = 0;
    
        // Has a pointer to the ThreadWrapper
        bool IsStopped()
        {
            return m_pThreadWrapperInst->IsStopped();
        };
    };
    
    // A User of our API is free to Derive from BaseClass and write its own execute
    class CExecuteClass : public CBaseClass
    {
        virtual void Execute()
        {
            // a proper execute should look like this
            while( ... && !IsStopped() ) ... 
    
            // not a proper execute and this is the problem
            while( ... ) // Some condition which never becomes true
            {
                ... Alloc huge amount of memory (about 10MB / s )
            }
        };
    };
    
    // a thread wrappe
    class CThreadWrapper
    {
        ...;
    
        unsigned ThreadHandlerProce
        {
            pExecuteClassInst->Execute();
        };
    
        //from extern
        void Stop()
        {
            //Lock Critical Section
            m_bStopped = true;
            //Unlock Critial Section
             
            if ( !WaitForStopped( 5 seconds ) )
            {
                // Terminate if not stopped
                Terminate();
            }        
        };
        
        bool IsStopped()
        {
            //Auto Lock Critical Section
            return m_bStopped;
        }
    
        // Stop Signal is a simple bool protected with a critical section
    private:
        bool m_bStopped;
    };
    
    // And somewhere in the code
    {
        //...
    
        pThreadWrapper->Stop();
        delete pThreadWrapper;
        // This is the described location, where it runs into the deadlock
    
        //...
    }
    .. hope this explains my problem a little bit more.

    Is it possible, that the following happens?
    When allocation/deallocating memory on the heap, the heap is locked.

    -The "Execute" allocates memory and locks the heap.
    -The Thread is terminated.
    -The Heap keeps locked.
    -At next allocation a deadlock occurs

    By,
    Matze

  5. #5
    Join Date
    Feb 2002
    Posts
    5,757

    Re: Strange Deadlock while terminating a thread

    In this case, the key is in the waiting stage. Does the second while loop rely on the same break key?

    Kuphryn

  6. #6
    Join Date
    Feb 2004
    Location
    Germany
    Posts
    153

    Re: Strange Deadlock while terminating a thread

    Hi kuphryn,

    if you mean the part
    Code:
    if ( !WaitForStopped( 5 seconds ) )
            {
                // Terminate if not stopped
                Terminate();
            }
    the answer is no.
    Here i ask for the exit-code of the thread. i.e.

    Code:
    while( GetExitCode() != STILL_ACTIVE )
    {
    Sleep(50);
    // check time waited yet
    }
    And generally it works perfectly.
    In the described case it works, too.
    But after waiting and then terminating the thread, it hangs in
    the "delete" of the thread-object.

    By,
    Matze

  7. #7
    Join Date
    Feb 2002
    Posts
    5,757

    Re: Strange Deadlock while terminating a thread

    Interesting. Walk through the deletion code. What line does it stop at?

    Kuphryn

  8. #8
    Join Date
    Feb 2004
    Location
    Germany
    Posts
    153

    Re: Strange Deadlock while terminating a thread

    Hi Kuphryn,

    first, thank you very much for your interest in this subject.
    second, i did a little mistake in my last post.
    sure it must be:

    Code:
    while( GetExitCode() == STILL_ACTIVE )
    {
    Sleep(50);
    // check time waited yet
    }
    now to the place it hangs in the delete:

    the file ( debug-version of course ): dbgdel.cpp
    and the code
    Code:
    void operator delete(
            void *pUserData
            )
    {
            _CrtMemBlockHeader * pHead;
    
            RTCCALLBACK(_RTC_Free_hook, (pUserData, 0));
    
            if (pUserData == NULL)
                return;
    
            _mlock(_HEAP_LOCK);  /* block other threads */ <--- Here ist hangs !!!
            __TRY
    
                /* get a pointer to memory block header */
                pHead = pHdr(pUserData);
    
                 /* verify block type */
                _ASSERTE(_BLOCK_TYPE_IS_VALID(pHead->nBlockUse));
    
                _free_dbg( pUserData, pHead->nBlockUse );
    
            __FINALLY
                _munlock(_HEAP_LOCK);  /* release other threads */
            __END_TRY_FINALLY
    
            return;
    }
    I checked the code below, and found that _mlock simply enters a special critical section.
    Because of this, my assumption is, that a earlier lock wasn't leaved by the terminated thread. So Entering the critical section isn't possible.

    Strange Subject, isn't it ?

    Bye,
    Matze

  9. #9
    Join Date
    Feb 2002
    Posts
    5,757

    Re: Strange Deadlock while terminating a thread

    Debugging thread deadlock via forum is difficult. Walk through the key sections of the thread and before and after the call to _mlock.

    Kuphryn

  10. #10
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Strange Deadlock while terminating a thread

    Quote Originally Posted by matze42
    Code:
     if ( !WaitForStopped( 5 seconds ) )
             {
                 // Terminate if not stopped
                 Terminate();
             }
    But after waiting and then terminating the thread, it hangs in
    the "delete" of the thread-object.
    How are you terminating the thread? Do you signal it to end via an event or do you use TerminateThread()? Also, I noticed that you have the m_bStopped param guarded with a critical section. What cs mechanism are you using? Native Win32 cs? CS wrapper class?

    Arjay

  11. #11
    Join Date
    Feb 2004
    Location
    Germany
    Posts
    153

    Re: Strange Deadlock while terminating a thread

    Hi Arjay,

    like described above i first try to signal the thread to finish itself, by setting the stop flag.
    And then if its not ended in between 5 sec. i terminate it via TerminateThread.

    The critical section is indeed a Win32 CriticalSection wrapper-class. But I'm using it for years and never encountered any problems with it, even in bigger applications with more threads.

    I know that its very difficult to debug such a problem via the forum, threrefore i try to reproduce the problem with a small project, which i can post totally here.

    Until then, thank you all for your interest,
    By,
    Matze

  12. #12
    Join Date
    Mar 2003
    Location
    Morocco
    Posts
    257

    Re: Strange Deadlock while terminating a thread

    Code:
    virtual void Execute()
        {
            // not a proper execute and this is the problem
            while( ... ) // Some condition which never becomes true
            {
                ... Alloc huge amount of memory (about 10MB / s )
            }
        };
    i think the deadlocks comes from this place.When u send ur stop signal ,nothing catches it so it's not handled .After when u terminate the thread and want to delete it ,memory is still allocated and may used by the thread
    We are the memories we let to other people
    Rate if i helped

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured