Unity3d Game development Multi-threading and using multithreading
Unity3d multi-threaded in. Threading is a fairly complex topic, but if mastered, it is easy to use multiple hardware processors or handle difficult partitioning of management data blocks.
such as in the scene with a * algorithm for a large number of data calculation, deformation grid operation of a large number of vertices, continuous to run upload data to the server, two-dimensional code recognition and other image processing, if you have to deal with a lot of things or interact with Unity object small can use thread, otherwise use coroutine.
Threads are run concurrently with other threads in the program. On multiprocessor computers You can do multiple threads of true synchronization, and more threads depend on having more than one processing core.
Unity3d when programming, there is always a main thread that executes your code, or you can create additional threads and the main thread to run concurrently. In unity, you can only access unity3d components, objects, and Unity3d system calls from the main thread. Any attempt to access the second thread of these items will fail and throw an error, which is a limitation to be attached.
So when you write code, you think a function starts and reaches the point where it executes, and the same thing you do is executed in another function, but it doesn't change. The operating system determines the execution of your code, and at any time, your code can only "hibernate" temporarily, and then let the other code begin to run.
Multithreading in Unity3d and using multithreading
In this example, when the first thread loads the value of a into the CPU register and prepares +1 for interruption, the second threads reads the value of a and subtracts 1000, at which point a should be-950, and now the first thread restarts, and the result of its 50+1 in the register is stored in A,a and becomes 51, And-950 has been thrown away.
Fundamentally, there are a number of precautions to take when using multiple threads to access variables or memory at the same time to ensure that this does not happen. So unity decided to access these variables from another external line or the memory is invalid, just to avoid problems with all system and framework objects. So make sure you have only one thread at a time to modify the variable, which doesn't mean you can't work with multithreading, you can use "sort" to solve the problem.
There is a lock keyword in C # to make sure that only one thread can access a particular object at a specific time, and here it says that the object is locked by a type value (value type) or prototype (primitive).
1. int a = +;
2.
3. Object guard = new Object ();
4.
5. void Threadonecode ()
6. {
7. //Some code in this
8.
9. Lock (guard)
Ten. {
A = a + 1;
. }
13.
///The rest of the code is in this
15.
. }
17.
void Threadtwocode ()
. {
///Some code in this
21st.
Lock (guard)
. {
a = a-1000;
. }
Copy Code
all are locked within the guard, guaranteeing that at the same time only one thread accesses it through the guard, you can use any suitable object. " Dog Planing Learning network " Now you may have a variety of problems, such as you want to lock more than one thing, may be nested in each other. So what do we do?
Our class is called loom, so you can easily run code on another thread, here are two features to note:
RunAsync (Action)-a set of code that runs on another thread.
Queueonmainthread (action,[optional]float time)-statement that runs on the main thread (optional delay).
Using Loom.current to access loom-to create an invisible gameobject to handle the interaction of the main thread of the game, the following example uses loom to update the results of all the vertex multiplication of a grid.
Unity3d Game development Multi-threading and using multithreading