Multithreaded Programming _1

Source: Internet
Author: User

The same process and thread, the scheduling of the thread is done by the operating system, the process of the task to the programmer to achieve their own implementation, of course, it can improve flexibility, and the cost of the co-process is smaller than the thread, in the program can open more co-process.

In Unity3d, a thread can be opened using Startcoroutine (String methodName) and Startcoroutine (IEnumerator routine). The difference is that using a string as a parameter allows the thread to be opened and terminated before the thread ends, whereas using IEnumerator as a parameter can only wait for the end of the thread to terminate at any time (unless the Stopallcoroutines () method is used), and when the string is used as a parameter. Only one parameter can be passed at most when the thread is turned on, and the performance consumption is a bit larger, while using IEnumerator as a parameter does not have this limitation.

In Unity3d, use Stopcoroutine (string methodName) to terminate a synergistic program, using Stopallcoroutines () to terminate all the cooperating programs that can be terminated. However, both of these methods can only terminate the Monobehaviour in this program.

There is also a way to terminate the co-program, set the active property of the Gameobject where the program is located to False, and when you set the active to Ture again, the co-program will no longer open It will not take effect if you set the enabled script for the collaboration program to false. This is because the co-operation is turned on as a thread, and Monobehaviour is a thread, they become non-interfering modules, unless the code is called, they work on the same object, only if the object is not visible to terminate the two threads at the same time. However, in order to manage our extra open threads, Unity3d put the call of the cooperative program in Monobehaviour, so that we can easily invoke the cooperative program in the specified script when we are programming, instead of being unable to manage it. In particular, it is easy to make mistakes in multi-person development by simply judging the thread based on the method name, which ensures that the object, the script is managed methodically, and the duplicate name is prevented.

Coroutine

Normal coroutine Updates is run after the Update function returns. A Coroutine is function so can suspend its execution (yield) until the given given yieldinstruction finishes. Different Uses of coroutines:

    • yield; The coroutine would continue after all Update functions has been called on the next frame.
    • yield waitforseconds (2); Continue after a specified time delay, after all Update functions has been called for the frame
    • yield waitforfixedupdate (); Continue after all fixedupdate have been called on all scripts
    • yield WWW . Continue after a WWW download have completed.
    • yield Startcoroutine (MyFunc); Chains the Coroutine, and would wait for the MyFunc coroutine to complete first.

================== Unity Multithreading

Some calculations that do not involve u3d APIs can be placed in sub-threads, which can improve the utilization of multicore CPUs. Summary: 0. Variables (all pointing to the same memory address) are shared 1. An API that is not Unityengine can run 2 on a split thread. Unityengine defines the basic structure (int,float,struct defined data type) that can be computed in sub-threading, such as Vector3 (Struct), but texture2d (class, the root parent class is Object) is not available.3Unityengine defines the basic types of functions that can be run on a sub-thread, such as

    1. int i = 99;
    2. Print (i.ToString ());

    1. Vector3 x = new Vector3 (0,0,9);
    2. X.normalize ();

The function of a class cannot be run on a sub-thread

Obj.nameis actually the Get_name functionSub-Threading Report error: Get_name can only is called from the main thread.

Texture2d tt = new texture2d (10,10);

Will actually call the internal_create in Unityengine.

Sub-Threading Report error: Internal_create can only is called from the main thread.

Other transform.position,texture.apply () and so on are not allowed to run in sub-threads. Conclusion: Sub-threading can do basic types of calculations, as well as non-unity (including. NET and SDK) API

Multithreaded Programming _1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.