3

What happens to internal threads after the main thread finishes execution?

E.G:

Program 1 creates 5 threads to run in background.
Program 1 crashes.
Are those threads alive?

They will get killed because threads are bound in the process context?

EProgrammerNotFound
  • 2,403
  • 4
  • 28
  • 59

2 Answers2

2

All those resources are linked up to the process data structure. When a process is terminated Windows ensures that all the resources are cleaned-up.

Didn't manage to find references to docs [feel free to add some, I am sure I read this in M.Russinovich "Windows Internals" book], but here are couple of similar answers regarding memory dealocations. Thread clean up is part of the clean up routine that windows call when a process is terminated.

https://stackoverflow.com/a/2975844/706456

https://stackoverflow.com/a/654766/706456

Community
  • 1
  • 1
oleksii
  • 35,458
  • 16
  • 93
  • 163
1

If a thread A creates a thread B and thread A terminates then thread B will not be killed as there is no parent/child relationship between threads.

The only situation where other threads are terminated is when the main process terminates (so, if the program crashes, they will get killed indeed).

The Coding Monk
  • 7,684
  • 12
  • 41
  • 56