When to call CloseHandle?

I have a main thread that takes actions that take some time. Therefore, I create a thread and delegate the task to it. This run function is called by the main thread when jobs are received. each work implements implementation.

Return_type execute( Arguments_here) { // if a file read case DWORD threadId; HANDLE hThread = CreateThread( NULL, // default security attributes 0, // use default stack size MyAsyncFileRead, // thread function name details, // argument to thread function 0, // use default creation flags &threadId); // returns the thread identifier // else do other work } 

Now that I do not want to wait for the main thread, I do not call WaitForSingleObject. [My knowledge of threads on Windows is low. therefore, if this is not required, I apologize]

If I wait for the thread to close, it will wait for my main thread. I do not want to do this. So when do I call CloseHandle?

When you have 10 jobs in hand and one creates 10 threads, and then waited for all 10 threads to complete, then it looks good for wait_for_multiple_objects, and then calls CloseHandle for each descriptor.

But what should I do in this case?

[I believe this question would be relevant for all OSs, so by tagging them. ]

+4
source share
2 answers

If you really don't care about waiting for the stream, you can really close the handle immediately after creating the stream.

However, I categorically do not recommend this. You should always wait until the threads are removed (preferably in a clear, well-defined way). If this has not been done before, wait for each thread that you spawned when the program exits. Always, no exceptions.
Do not leave main without knowing if other threads continue to run. If necessary, kill them in a difficult way (although it is desirable, let them be gracefully controlled and wait for him).

If you do not wait for the threads to finish, you may see strange exit failure conditions. Or, even worse, you may not see them, and only users / clients complain that once a hundred times the configuration file is damaged (or, even worse, the data file) 1 . Now imagine that they can demonstrate step by step what they are doing, and you can say that they are doing everything right, and there is no way for something to go wrong. Good luck with the fact that the accident is caused by the fact that the workflow is working on some object (or global state) that was simply removed from the main main thread, explicitly or implicitly using a CRT.

Of course, your position may be that the workflow will exit long before the program ends anyway, so why bother. However, it plays Russian roulette.


1 This is not fiction, but something that I really saw before.
+5
source

You can use unique_ptr<HANDLE, CloseHandle> threadHandle - if you have a suitable place to save / place the threadHandle variable ..

Or, if you don’t really need a descriptor, just close it immediately after starting the stream, since the descriptor is really useful if you take care of the stream later.

From the MSDN CreateThread docs:

The thread object remains in the system until the thread is empty and all calls to it have been closed by calling CloseHandle.

If at some later stage you need a thread descriptor, you can always use OpenThread to get a β€œnew” descriptor. Assuming, of course, you have not "lost" threadId .

Of course, the correct solution in C ++ is to use std::thread .

+1
source

Source: https://habr.com/ru/post/1499134/


All Articles