To perform a specific task, I had to resort to creating an additional stream. I create a new thread in a windowed application.

D. Richter recommends using the _beginthreadex() and _endthreadex() functions

 HANDLE hThread; unsigned threadID; hThread = (HANDLE)_beginthreadex(NULL, 0, &ThreadFunc, NULL, 0, &threadID); WaitForSingleObject(hThread, INFINITE); CloseHandle(hThread); 

stream function:

 unsigned __stdcall ThreadFunc(void* pArguments) { while(true) { //... } _endthreadex(0); return 0; } 

As a result, the thread is started, its code is executed, and the WaitForSingleObject function waits for it to finish, in order to close its handles later.

The problem is that this WaitForSingleObject hangs the window itself. Question: if you remove WaitForSingleObject and CloseHandle(hThread) from the code, how else should you wait for the thread to close in order to close its handle without hanging the main program?

  • second parameter WaitForSingleObject - timeout - VTT
  • And the point is to block the wait for the stream, in this case the code can be run without a stream. If this is a normal window application, let the thread send a message to the main window saying that it has worked, and the main thread can then call WaitForSingleObject and clear the resources. - KoVadim
  • @VTT, the second parameter sets the wait interval. In this case, I indicated INFINITE - infinity, because It is not known when the flow will end. This option is not appropriate (because the main thread hangs up), but in some other way you need to let the main thread know when this thread is finished and call CloseHandle (hThread). Your options? - Ig_M
  • @KoVadim, yes, of course, there is no point to block. I gave only an example that needs to be changed. Did I understand correctly - you propose in the flow function after _endthreadex (0) to send a message to the main window, for example, SenMessage (mainHwnd, WM_MYEXITTHREAD) and call CloseHandle (hThread) in the message handler? - Ig_M
  • You can periodically call WaitForSingleObject with a 0 timeout until the stream is terminated. Then the main thread will not be hung. - VTT

1 answer 1

One gets the impression that you don't need the handle at all, and you just want to close it. If so, you can call WaitForSingleObject immediately before exiting the program. Or you can not close it at all - all unclosed handles are closed by the system at the end of the program.

Or even use _beginthread . Because Richter does not recommend using it, it does not follow at all that this function is damaged and does not work well. Richter in his book justified why he does not advise it - let's see what this function did not like to him.

  1. _beginthread does not allow a separate security descriptor to be set for a thread. I won’t even ask if you need this.

  2. _beginthread does not allow creating a thread in the inactive (suspended) state. Do you use it?

  3. _beginthread automatically closes the thread handle at the end of the thread function. Perhaps this is just what you need?

In general, it is necessary to be critical of the recommendations of the authorities. Many of them are not based on anything, but reflect the personal preferences and phobias of these very authorities. Speaking specifically about _beginthread - it’s no worse than _beginthreadex , it just works a little differently. In some cases it is more convenient, in some - _beginthreadex . You just need to understand their features and applicability in each case.

  • It is not an option to close the handle once before exiting the program. if necessary, the stream can be restarted. And, therefore, every time at its completion, you need to close the handle. I did not claim that I did not want to use _beginthread, but I would like to deal with _beginthreadex. - Ig_M
  • Here is an example: suppose a thread is started by the _beginthreadex function. The thread function every 5 seconds (while + Sleep ()) checks the value of the global flag IS_STOP. In the program, press the button that assigns IS_STOP = TRUE. And WaitForSingleObject are waiting for completion. The program will hang until the "timer" in the stream counts down 5 seconds and checks IS_STOP. Then the handle will be closed. Subsequently, the thread can be started again. How to be in this case? - Ig_M
  • @Ig_M, and why do you need to immediately call WaitForSingleObject ? Put a flag and go about your business, let the stream finalizes its 5 seconds. Call WaitForSingleObject when you really need to wait for the thread to complete, for example, before restarting it. - freim