Windows C ++ - closing thread with CloseHandle

I created a thread using the CreateThread function.

in this thread, I have a while (true) loop (which reads input).

Now that I want to close the stream, I use the "CloseHandle" function.

Is this right to do? Or should I exit the while (true) loop and then use the CloseHandle function?

thanks

+6
source share
3 answers

CloseHandle () does not kill, terminate or support the thread, it destroys the handle itself (so you do not have a handle to kill the thread or wait for it). The thread continues to work fine (I used this in many cases), and the only way to stop it is to either exit the thread function (ThreadProc ()) or kill it.

+8
source

Typically, this (calling TerminateThread) is bad because the thread may allocate some resources (i.e. file descriptors) that will not be available until the whole process is complete. Moreover, CloseHandle does not stop the thread.

If you have a long operation inside your thread, then at least use

while(!ShouldExit) { DoOneMoreIteration(); } 

cycle. That way, you can terminate the stream by setting IfExit to 1 (or "true" if it is a C ++ and bool variable) and calling WaitForSingleObject in this stream descriptor to make sure it is complete.

For the comment, eran: ShouldExit should be declared as "volatile".

If you expect input (the console, I suppose), you can use non-blocking ("overlapping") I / O with standard input.

For example, see this question: Checking Win32 File Streams for Available Input

It will be something like

 HANDLE h = GetStdHandle(STD_INPUT_HANDLE); while(!ShouldExit) { if(WaitForSingleObject(h, FALSE, SomeTimeoutValue) == WAIT_OBJECT_0) { ... use std::cin - it has some input and won't block } } 

To do something better (avoid processor overflows), use WaitForMultipleObjects and go beyond the loop on some external event.

 /// Global var HANDLE SomeGlobalEvent; /// ThreadProc(): HANDLE h[2]; h[0] = GetStdHandle(STD_INPUT_HANDLE); h[1] = SomeGlobalEvent; while(true) { DWORD which = WaitForMultipleObjects(2, h, FALSE, SomeTimeoutValue); if(which == WAIT_OBJECT_0) { ... use std::cin - it has some input and won't block } else if(which == WAIT_OBJECT_1) { // got the termination event break; } } /// "Main" thread: SomeGlobalEvent = CreateEvent(NULL, false, false, NULL); HANDLE hThread = _beginthread(ThreadProc, 4096, NULL); .... /// send termination signal SetEvent(SomeGlobalEvent); /// Wait for thread completion WaitForSingleObject(hThread); 
+6
source

It is best to carefully read the documents. The Win32 API is well documented.

from MSDN to CreateThread in the Remarks section says

Flow execution begins with the function specified by the parameter lpStartAddr parameter. If this function returns, the return DWORD value is used to terminate the thread in an implicit ExitThread function call. Use the GetExitCodeThread function to get the return value of the thread.

Following this, you must let the stream input function do its job and finish. That is, exit the loop that will return the stream input function.

+3
source

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


All Articles