0

Exit(0) and detach thread

Hi Let me clarify my understanding first: I have created thread objects (not the jthreads) and checker for joinable. If joinable, I try to detach them so thread works even after main thread is completed. If I want main thread to wait, I will use join() instead of detach() on thread object. This is normal flow and I believe my understanding is correct. Now, what if I have a exit(0) in main thread before join or detach is called on thread objects? Will all threads be cancelled irrespective of join or detach due to exit(0)? Will have clean exit (even though we had exit(0)) or not due to thread not joined or detached yet?

14th Sep 2024, 6:13 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
4 Respuestas
+ 1
I have to try this on my ide to understand how it works. I think you're correct. exit(0) does not respect thread cleanup. All thread regardless of what they are, mutexes etc are forcefully terminated. This means you won't have a clean exit since they've not been detached afterall
14th Sep 2024, 9:22 PM
RuntimeTerror
RuntimeTerror - avatar
0
Thanks RuntimeTerror How to verify if it's a clean exit or not on an ide
15th Sep 2024, 5:53 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
What ide do you use? It's pretty much obvious on jetbrains clion
15th Sep 2024, 1:36 PM
RuntimeTerror
RuntimeTerror - avatar
0
Visual studio
15th Sep 2024, 3:09 PM
Ketan Lalcheta
Ketan Lalcheta - avatar