I’m just getting into the concept of threading, i have a few questions to ensure i’m understanding it correctly.
Am i right in thinking that a seperate thread will allow my game to use a seperate cpu core on the host computer, if it has one? And if i ran seven additional threads, i’d theoretically be able to fully utilize every core of an 8-core cpu ?
Will stuff done in a seperate thread affect the main unity thread, if they’re runnning on seperate cores? If i ran an infinite loop in a single frame for instance, the game would normally lock up, but if it’s on a seperate thread will it just continue running away without affecting the main one?
And also, will that apply on a singlecore system (if such things even exist nowadays)
Yes of course. Every thread runs independently from each other. Threads are also not bound to “frames”, only the main thread is since it’s the one that actually creates one frame each iteration in it’s main loop. On single core CPUs it works exactly the same just that only one thread runs at a time (since there’s only one core). All threads are scheduled in time-slices.
That all said threads are awesome, but keep in mind that the whole Unity API is not thread safe. So you can’t use any method of any Unity object in another thread. You can use threads if you want to process a lot of data (terrain or mesh generation or texture processing). You can’t use SetPixels or assign mesh data to a Mesh object since that’s all part of the Unity API. So you would need to transfer the data synchronised to the main thread to actually use them in your game.
If you’re new to threading make sure you undersand the purpose of synchronising and locking completely. Using threads in the wrong way is the easiest way to shoot yourself in the foot. Actually you don’t really shoot yourself in the foot but it’s like firing tons of bullets in a closed rubber room so the chances are quite high one of the bullets will hit you. It’s near to impossible to debug such a program. That’s why it’s important to always go the safe route when using threads.