-
February 18th, 2020, 08:44 AM
#16
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.
-
February 18th, 2020, 12:03 PM
#17
Re: Stop a thread
Originally Posted by mesajflaviu
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).
-
February 19th, 2020, 03:56 AM
#18
Re: Stop a thread
Originally Posted by mesajflaviu
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
-
February 19th, 2020, 11:40 AM
#19
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".
-
February 19th, 2020, 12:57 PM
#20
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)
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|