- Home /
What does new Thread do, and how many threads are too many?
What does new Thread(functionName) do?
If I try new Thread(functionA); new Thread(function B); would both function A and B be running at the same time on different threads?
... and how many new threads can you run? Is there a way to do a try catch for this, to check if the thread can be started without danger?
Answer by asafsitner · Jul 08, 2012 at 08:13 AM
new Thread(functionName)
does exactly what you expect it to - it starts a new thread that runs the specified function. So in your example yes, functionA and functionB would be running in parallel on different threads.
This can cause very interesting bugs, especially when more than one thread tries to access the same resource.
If you want to delve in multi-threading, which is a fascinating and extremely useful aspect of modern programming, I suggest you start with this tutorial and go from there.
HOWEVER! Unity is strictly single-threaded and from what I've seen trying to start a new thread from within Unity will cause everything to explode (i.e. give an error and stop working. It wouldn't, you know, REALLY explode...)
There is a theoretical limit of 2048 threads on a 32-bit windows system.
There is also a theoretical limit of threads in a 64-bit windows system, however it is obviously much larger and there is no reason you should ever need to reach it.
As you can guess, the thread count is limited by available memory.