c++ - 当我终止进程时,它也会终止线程吗?

标签 c++ multithreading winapi process

据我所知,线程是进程的一部分。如果关闭进程,我需要手动关闭线程吗? 我的代码:

TerminateProcess(hProcess, 0);
CloseHandle(hProcess);

我看过示例代码,其中关闭进程然后线程:

CloseHandle(l_processInfo.hProcess);
CloseHandle(l_processInfo.hThread);

最佳答案

根据How Threads are Terminated ,

A thread executes until one of the following events occurs:

  • The thread calls the ExitThread function.
  • Any thread of the process calls the ExitProcess function.
  • The thread function returns.
  • Any thread calls the TerminateThread function with a handle to the thread.
  • Any thread calls the TerminateProcess function with a handle to the process.

还有

The TerminateThread and TerminateProcess functions should be used only in extreme circumstances, since they do not allow threads to clean up, do not notify attached DLLs, and do not free the initial stack. In addition, handles to objects owned by the thread are not closed until the process terminates.

关于c++ - 当我终止进程时,它也会终止线程吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68076338/

相关文章:

c++ - 如何在 GLFW 中为游戏引擎创建正确的输入类

java - 如何在不被阻塞的情况下分块读取Java中的大文件?

c++ - 使用 OpenCV 在 iOS 应用程序中进行圆检测

c++ - 使用 (void)someInt 的原因;在代码中

java - 哪个并发列表适合多个作者一个读者?

c++ - 拦截 WM_CLOSE 进行清理操作

winapi - 在 Windows 中创建现代风格的动态菜单

windows - 使用 MS CryptoAPI 保存/恢复证书会使附加的私钥无效

c++ - 句柄类和赋值运算符

java - 何时更喜欢 LinkedBlockingQueue 而不是 ArrayBlockingQueue?