Ensuring a thread is killed

I’m trying to figure out when all of my threads die.

My application has, for instance, two threads running aside from the main Unity thread. One to ingest data from an external source, and one to push that data to a server.

So far, I have found that threads don’t end when Unity Editor mode ends and created a type of heartbeat to keep them alive, but this application will also run on mobile devices. So, my question is: On mobile devices, will threads created by Unity be sandboxed with Unity so they are killed at the same time, or do I need to explicitly make sure they are aborted as well?

Well, threads in Unity are managed threads which are killed with the AppDomain when the application is terminated. However i’m not sure what happens when an app is suspended. It probably depends on what kind of mobile device you talk about since the behaviour is more dependent on the underlying operating system (iOS, Android, WindowsPhone).

Why don’t you create a test application, start a thread which might write periodically into a log file or something similar and test all those cases?

Anyways it’s always better to terminate your threads yourself. If terminated by the OS it can be terminated in any state. A thread should terminate itself based on some external / global state. See this for reference.

If you have to ensure the thread is terminated you can also call Abort on your thread, however that shouldn’t be the “normal” way to terminate your thread. Abort just raises an exception inside the thread which might result in terminating the thread.

You should read the remarks and notes on Thread.Abort carefully.