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

Thread: Stop a thread

  1. #16
    Join Date
    Jan 2009
    Posts
    399

    Re: Stop a thread

    The point is that function is working well as long I got no ML when I let thread to terminate by itself.

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

    Re: Stop a thread

    Quote Originally Posted by mesajflaviu View Post
    The point is that function is working well as long I got no ML when I let thread to terminate by itself.
    Our point is you need to exit cleanly from the thread so it can cleanly terminate. If you are calling multiple functions inside a thread then check for the exit event after each function call. If functions are long running and cannot be cancelled, then use different functions.

    GetFileAvailability doesn't seem to be a c library function. What does it do? If it sits in a tight loop, waiting for a file to show up, then that is a problem. If it does this, then rewrite the function so you can check for the exit event inside the loop. Or better yet use a file watcher functionality which is non-blocking and you get a notification when a file gets created (or updated, or deleted, etc.). Rethink the problem and use more modern approaches (vs. C library functions that may be blocking).

  3. #18
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Stop a thread

    Quote Originally Posted by mesajflaviu View Post
    The point is that function is working well as long I got no ML when I let thread to terminate by itself.
    Well, then don't allow the main thread to exit until all your working threads will exit.
    It is not so hard to implement:
    • every worker thread by its start posts the user defined message to the main thread window notifying about thread has started;
    • every worker thread just before its exit posts the user defined message to the main thread window notifying that thread is about to exit;
    • the main thread implements the thread counter, it increase the counter after receiving the "thread has started" message and decrease the counter after receiving the "thread exits" message;
    • the main thread does not exit until the counter becomes zero. It can also display some modeless dialog notifying the user why the App does not exit and what is still waiting for.
    Victor Nijegorodov

  4. #19
    Join Date
    Jan 2009
    Posts
    399

    Re: Stop a thread

    Thank you Victor for your summarize and synthetize the thread work. Now I am seek to solve "every worker thread just before its exit posts the user defined message to the main thread window notifying that thread is about to exit;" in order to "the main thread does not exit until the counter becomes zero".

  5. #20
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Stop a thread

    Another way to know if all the threads have terminated is to determine how many running there are and if > 1 then there are other threads running. Consider:

    Code:
    #include <windows.h>
    #include <tlhelp32.h>
    
    [[nodiscard]] auto num_threads()
    {
    	if (const auto ss {CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)}; ss != INVALID_HANDLE_VALUE) {
    		const auto cp {GetCurrentProcessId()};
    		PROCESSENTRY32 pe32 {sizeof(PROCESSENTRY32)};
    
    		for (auto cont {Process32First(ss, &pe32)}; cont; cont = Process32Next(ss, &pe32))
    			if (pe32.th32ProcessID == cp)
    				CloseHandle(ss);
    
    		return pe32.cntThreads;
    	}
    
    	return 0UL;
    }
    
    int main()
    {
    	cout << num_threads() << endl;
    }

    The function num_threads() will give the current number of running threads for the current process. If this value is 1 then there is only the main thread running. If the return value is 0 then there is an error.
    Last edited by 2kaud; February 20th, 2020 at 06:32 AM.
    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++23 Compiler: Microsoft VS2022 (17.6.5)

Page 2 of 2 FirstFirst 12

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
  •  





Click Here to Expand Forum to Full Width

Featured