This time the question is simple: can I use threads in Monobehaviour extended classes?
I would to know if Unity has a own threads system or if I need to use the C# threads.
I also tried Invoke, InvokeRepeating and StartCoroutines but my experiences tell me that these methods are not standalone thread but delayed time execution.
Sorry to make you bother, but could you please make me an example?
To tell you the truth I tried to add a thread in one of my class, but after 2 calls Unity will crash. I rally don’t know what could be the problem.
Basically, as you said, Unity is NOT thread safe so, I presume, that there is only a thread that calls all the Monobehaviour extended class methods (like Awake, Start, Update, etc…) at different Time.time value that is like a counter that increments its own value every time the thread has finished to call all methods. That means that if I make a cycle in one of my component, the Time.time value is the same until the operations are fully completed.
You can see this in the project I’ve attached to this post.
On top of that I think that the engine computation could be very hard because there are no concurrent executions and all is done by a single thread.
The Unity API is not thread safe. You can still do thread-safe Mono stuff in threads no problem. As long as you avoid calling any Unity-specific APIs in threads, you should be fine.
I think is not possible to not touch Unity component or Unity API if I need a thread to compute some data. For example the sounds: if I need to create a sound effect changing its length by only looping its central part, it will be not possible to make a parallel computation of the sound cursor position and loop duration without touching the Unity audio library !!!
Well, I used threads in Fractscape to speed up auto-texturing calculations, and conversion to Unity terrain calculations. These are about 50% faster with threads than without threads on my machine, so it works pretty well. (Also used another thread for fractal terrain calculations, but only so the progress bar could be easily done, since the particular way I did the fractals isn’t really transferable to more than one thread. The other routines are only split into two threads for now; could use some work for an arbitrary number so those monster 8-core Mac Pros have something to do. ) I just do all the computations on arrays in threads, and then call any Unity APIs I need when the threads are finished.
which one is “the main thread”?
As unity has not a single entry point I’ve some problems to understand what you meant with that.
Or is the idea to only run and manage threads from a single (static) object?
The “main thread” is the one that calls your “Update()”, “LateUpdate()” and all the other methods in your MonoBehaviours. It’s also the one that calls all your Coroutines (which kind of look like threads but really are executed inside of that "main thread as some sort of Iterator).
As Eric pointed out: If you start a thread, you need to make absolutely sure that anything that is invoked from this thread does not access the Unity API at all. If you do, you’re likely to get crashes, and they might be ugly to debug. So you need to be super-careful.
However, you can “ask” from the main-thread about changes that your own threads have done. In other words: You might have your own object which can be accessed both by your own thread and your game object. Then, your own thread can change the state of that object, and your game object can check for that changed state (and call into the Unity APIs without a problem). “game object” = “script attached to a game object”
This is very interesting… but it doesn’t solve my problem: I’ve to access with a thread, every frame, to the GameObject audio component. Also if I try to think to a solution about dividing the operation I need to do, I cannot find a solution.
However, Jashan, if you could be so kind to make me also a very simple example, I will try to solve this.
THANKS !!!
Here is a simple project of doing multithreaded Unity. You have to remember to not call any Unity APIs in your spawned threads. In this example I’m using a synchronized object to pass calculated data to the Unity API.
Your examples are really exaustive… but for real I think that the moment that we’ll need to use apart thread with Unity APIs will come… it’s only a matter of time !!!