Async in Unity
Recently, in the process of using Unity, various problems involving asynchronous calls in some high concurrency states have arisen, so there are some doubts about asynchrony in Unity, but in the end, the most important conclusion is first thrown, asynchronous is not equal to multithreading.
First of all, we need to understand that C #in unity is different from C #we usually say. The C # we usually say that his running virtual machine is .Net Framework, while Unity is Mono VM, so the syntax of the two It looks the same, but the actual principle may not be the same, such as await to be understood later in this article, and Mono’s syntax support for C #is only 2.8, and some new C #syntax cannot be used. For the specific comparison of the two and what IL and IL2CPP are, you can refer to this article:Unity将来时:IL2CPP
In fact, when I first saw these two keywords, as a person with a little Web front-end development experience, my first reaction was to compare with JS. In JS, these two keywords started a new coroutine on the main thread of JS. coroutine.
So is this true in dotnet and unity? Are they coroutines? Are they coroutines of the main thread?
If you don’t understand what a coroutine is, you can search for it yourself. I will explain the relationship between processes, threads, and coroutines in a more official language (well, it doesn’t seem to be very simple), which is:
A process is a unit of computer resource scheduling other than the CPU. You apply for a piece of memory and a device, all of which are based on the process. Therefore, the exchange of information between processes requires special processing because they are completely isolated. This process is called Inter-Process Communication.
After the process has applied for resources, it is a thread to apply for CPU resources. In theory, core solo CPU is a process that only one thread is running at the same time. The so-called multi-threading means that the CPU may select a thread for execution through a certain scheduling algorithm within a period of time. This switching is automatic by the operating system, so the system needs to automatically maintain the context of the thread executed before, such as which line was executed last time. However, the multi-threads of the unified process share memory, so there is no so-called inter-thread communication.
The above switch requires the system to help maintain the context of the thread, which consumes a lot, so there is a way for the thread to maintain the context itself and then switch the execution content, which is called a coroutine. For example, now the CPU has allocated a time slice to me, and I haven’t used it up yet, but the current task is blocked because I need to wait for something, and I don’t want to give up the time slice, so I switch internally. This process does not require system participation.
Coroutine
Keywords
This keyword is not unique to Unity, unity also comes from c #, so it is more appropriate to find an example of c #to understand. First look at the definition of IEnumerator:
1 | public interface IEnumerator |
It can be understood from the definition that an iterator has three basic operations: Current/MoveNext/Reset. Here is a brief description of the process of its operation. In common collections, when we use enumeration operations such as foreach, at the beginning, the enumerator is set before the first element of the collection, and the Reset operation returns the enumerator to this position.
When iterating, the iterator will first execute a MoveNext. If it returns true, it means that there is an object at the next position. Then set Current to the next object at this time, and Current points to the next object at this time. Of course, how c #compiles this IEnumrator into an object example to execute, will be explained below.
Keywords
The yield keyword in C #is followed by two basic expressions:
1 | yield return <expresion> |
Yield break is the operation of jumping out of the coroutine, which is generally used where an error is reported or where it is necessary to exit the coroutine.
Yield return is a relatively common expression. The specific expresion can be as follows:
1 | WWW: Common web operation, called at the end of each frame, checks isDone/isError and if true, calls MoveNext |
Well, with an understanding of a few keywords, let’s look at how the C # compiler generates the coroutine call compile that we wrote.
C #compile results for coroutines
1 | class Test |
The result after compiling into C++ is:
1 | internal class Test |
So when we start a coroutine, its essence is to return an iterator instance, and then in the main thread, every time update, this instance will be updated to determine whether it performs the MoveNext operation, if it can be executed (such as file download is completed), then execute MoveNext and assign the next object to Current (MoveNext needs to return true, if false indicates that the iterative execution is completed).
Through this, we can get a conclusion that the coroutine is not multi-threaded, its essence is still executed in the main thread of Unity, and each update will trigger whether to execute MoveNext.
[Unity
This is the official doc of Unity for its coroutine. There are several key points:
Any variable or parameter can be correctly kept between yield statements.
By default, coroutines will resume on frames after performing yield, but can also be used WaitForSeconds To introduce Time Lag
Many tasks in the game need to be performed regularly. The easiest way to think of it is to include the task in the Update function. However, usually, the function will be called multiple times per second. When you don’t need to repeat the task with such frequency, you can put it in a coroutine for regular updates instead of updating every frame
Coroutines are similar to a life cycle function that is called at a fixed time and cannot be understood as coroutines of the operating systemOnce the gameobject where a coroutine is located becomes inactive, the coroutine will be interrupted, and even if it becomes active again, the coroutine will not continue
Can be used StopCoroutine And StopAllCoroutines To stop the coroutine. when used SetActive(false) When you disable a coroutine attached to a game object, the coroutine also stops. A call to Destroy (example) (where example is a MonoBehaviour instance) immediately triggers OnDisable and processes the coroutine, effectively stopping it. Finally, OnDestroy is called at the end of the frame.
By putting on a MonoBehaviour instance enabled When set to false to disable MonoBehaviour, the coroutine will not stop.
Task
Tasks in C
https://docs.microsoft.com/zh-cn/dotnet/standard/asynchronous-programming-patterns/
https://docs.microsoft.com/zh-cn/dotnet/api/system.threading.tasks.task?view=net-6.0
Simply put, Task just indicates that this thing is asynchronous, it does not necessarily start another thread to do it
Moreover, this Task, whether it is used or maintained in its internal state, is very similar to the JS Promise.
Await is used in conjunction with Task, await is Task, and async indicates that there will be await in this function.
Unity中的Task
Compared with C #Task, Unity Task has a feature that the context before and after await is the context of the main thread, which ensures that it can be called to Unity’s API, so it also brings some problems