CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 20

Thread: Stop a thread

  1. #1
    Join Date
    Jan 2009
    Posts
    342

    Stop a thread

    Please explain me why the following thread when is forced to stop is not cleaning the heap:
    // here is starting thread
    Code:
    BOOL CMyDoc::ReadHDD(CStringA sVolume)
    {
    	THREADSTRUCT* pTS = new THREADSTRUCT;
    	pTS->m_pDoc = this;
    	pTS->m_sVolume = sVolume;
    	m_pWinThread = AfxBeginThread(&CMyDoc::ReadThread, (LPVOID)pTS, THREAD_PRIORITY_BELOW_NORMAL, CREATE_SUSPENDED, 0, NULL);
    	m_pWinThread->m_bAutoDelete = TRUE;
    	m_pWinThread->ResumeThread();
    
    	return TRUE;
    }
    and
    Code:
    UINT CMyDoc::ReadThread(LPVOID lpParam)
    {
    	THREADSTRUCT* pTS = (THREADSTRUCT*)lpParam;
    
    	if (NULL == pTS)
    		return 0;
    
    
    	//.... code that takes long time, no returning call
    
    	delete pTS;
    
    	return 1;
    }
    where
    Code:
    // inside my CMyDoc header
    
    protected:
    	CWinThread* m_pWinThread;
    
    	struct THREADSTRUCT
    	{
    		CMyDoc* m_pDoc;
    		CStringA m_sVolume;
    	};
    what is happen: if I let that the thread to naturally close it, I mean after the thread is ended and afterwards close my app, everything is ok, I got no memory leak. But if I close my app when the thread is alive (while code that takes long time from the thread is executing), I got memory leak, of course due to pTS object from the heap.

    My question, what is happen when the thread is stopped while that "code that takes long time" is alive ?

    And how to be assured that pTS is properly delete it ?

    P.S.
    Of course, I can live without THREADSTRUCT, it is just for testing and learning purpose.
    P.S.2
    Of course, I can make pTS as CMyDoc member and delete it inside CMyDoc destructor. But I want to understand what happen in this situation.
    Last edited by mesajflaviu; February 17th, 2020 at 06:39 AM.

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,151

    Re: Stop a thread

    Thread when is forced to stop
    How is the thread stopped? If it is terminated elsewhere, then the heap memory is not automatically freed. Threads need to terminate themselves. If it is required that one thread terminates another then this is usually done by signalling the thread that it is to terminate. See http://www.flounder.com/processes.htm for more info.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.6.3)

  3. #3
    Join Date
    Jan 2009
    Posts
    342

    Re: Stop a thread

    "Thread when is forced to stop" I mean I close the whole app when thread is alive.

  4. #4
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,736

    Re: Stop a thread

    Before exiting the main App you have to let your worker threads exit.
    Your main thread can SetEvent for exiting an the worker threads have periodically wait for this event and if it has been set then exit (gracefully!) and notify the main thread about the exiting.
    See also about Using Worker Threads
    Victor Nijegorodov

  5. #5
    Join Date
    Jan 2009
    Posts
    342

    Re: Stop a thread

    "worker threads have periodically wait for this event" this one I don't know how to implement it ... because that code from the thread is in fact a simple call:
    Code:
    UINT CMyDoc::ReadThread(LPVOID lpParam)
    {
        ....
        some_function_that_takes_long_time();
        ...
    }
    where should I put WaitForSingleObject ? Before this function, or after ? I guess after ... but this mean that my app is not closing until this thread is done ?
    Last edited by mesajflaviu; February 17th, 2020 at 08:14 AM.

  6. #6
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,736

    Re: Stop a thread

    I have posted you a link to a great Joe Newcomer's essay about using worker threads.
    Please, check it out!
    Victor Nijegorodov

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

    Re: Stop a thread

    You need to restructure your code. Use an exit event as others have mentioned and inside the
    Code:
    some_function_that_takes_long_time();
    function, frequently check if the exit event has been set. If it has return a false from the function, and exit the thread if the function has returned false.

  8. #8
    Join Date
    Jan 2009
    Posts
    342

    Re: Stop a thread

    The fact is that some_function_that_takes_long_time function is a simple call into a C library which is not mine ... I don't know what option do I have ...

  9. #9
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,736

    Re: Stop a thread

    Quote Originally Posted by mesajflaviu View Post
    The fact is that some_function_that_takes_long_time function is a simple call into a C library which is not mine ... I don't know what option do I have ...
    And what is its name?
    And what is that function supposed to do?
    Victor Nijegorodov

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

    Re: Stop a thread

    Quote Originally Posted by VictorN View Post
    And what is its name?
    And what is that function supposed to do?
    And does that function have an alternative that allows it to be cancelled?

  11. #11
    Join Date
    Jan 2009
    Posts
    342

    Re: Stop a thread

    Quote Originally Posted by VictorN View Post
    And what is its name?
    And what is that function supposed to do?
    That function read specific file on HDD drive. Its name: GetFileAvailability(...)

  12. #12
    Join Date
    Jan 2009
    Posts
    342

    Re: Stop a thread

    Quote Originally Posted by Arjay View Post
    And does that function have an alternative that allows it to be cancelled?
    No, has no option to be cancelled ... I guess that putting a WaitForSingleObject after his call will solve ML issue ... is a step forward ...

  13. #13
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,736

    Re: Stop a thread

    Quote Originally Posted by mesajflaviu View Post
    That function read specific file on HDD drive. Its name: GetFileAvailability(...)
    So it is a user defined function, isn't if?
    Why does it require long time to run? How does it get/check the "FileAvailability"?
    Could you use some asynchronous function instead?
    Victor Nijegorodov

  14. #14
    Join Date
    Jan 2009
    Posts
    342

    Re: Stop a thread

    some_function_that_takes_long_time is a function inside a C library ... I have the source code, but inside is a long string of C functions that read some specific files on HDD device. In his reading process, deep inside, maybe I could put my code, but is not an easy thing due to language barrier. Strange to me is that when I stop my app while this function is alive, the thread is stopped before arrive to his ending ...

  15. #15
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,151

    Re: Stop a thread

    If GetFileAvailability() is not doing as expected (eg not returning etc), then it's likely that there is a problem with that function. Have you stepped through that function with the debugger to see why it is not returning within an expected time? It sounds like there is an infinite loop somewhere that is keep retrying some action until it succeeds which doesn't happen.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.6.3)

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)