An example analysis of C ා iterator and unity coroutine

Time:2020-9-30

1. C ා iterator

1.1. IEnumerable and ienumerator

Iterators in C are encapsulated in IEnumerable and ienumerator and their generic interfaces.

IEnumerable: defines a method that can get ienumerator — getenumerator().

//Code implementation of IEnumerable
  public interface IEnumerable
  {
    IEnumerator GetEnumerator();
  }

Ienumerator: iterates iteratively over the target sequence until the data is no longer needed. A common foreach loop uses this interface

//Code implementation of ienumerator
  public interface IEnumerator
  {
    /// <summary>
    ///Gets the current item (read-only property)
    /// </summary>
    object Current { get; }
 
    /// <summary>
    ///Moves the internal position of the cursor forward
    /// </summary>
    ///< returns > successful or not
    bool MoveNext();
 
    /// <summary>
    ///Resets the cursor before the first member
    /// </summary>
    void Reset();
  }

But why are there two interfaces? Why not just let IEnumerable implement all the methods?

In order not to violate the principle of single responsibility. If there are several nested foreach loops, each other may be affected.

1.2 use of iterator

Arrays and collections in C inherit IEnumerable interface, so we can use them directly.

static void Main(string[] args)
{
  int[] arr = new int[5] { 12, 65, 749, 16, 49 };
  //foreach (int item in arr)
  //{
  //  Console.WriteLine(item);
  //}
  #Region is the same as foreach above
  IEnumerator e =  arr.GetEnumerator (); // get the object to be iterated
  While (e.movenext()) // read the elements one by one. After reading, it will return false
  {
    Console.WriteLine (e.current); // output read content
  } 
  #endregion
}

Iterators are used in custom classes.

/// <summary>
	///Zoo class. There are many animals in it
	/// </summary>
	public class Zoo : IEnumerable
	{
	  Animal[] animals = new Animal[3];
	  public Zoo()
	  {
	    Animals [0] = new animal ("Zhang San");
	    Animals [1] = new animal ("Li Si");
	    Animals [2] = new animal ("Zhao Wu");
	  }
	  public IEnumerator GetEnumerator()
	  {
	    return this.animals.GetEnumerator();
	  }
	}
	/// <summary>
	///Animals. Each animal has its name
	/// </summary>
	public class Animal
	{
	  private string name;
	  public string Name { get { return name; } }
	  public Animal(string name)
	  {
	    this.name = name;
	  }
	}
  static void Main(string[] args)
  {
    Zoo zoo = new Zoo();
		//You can use foreach iteration directly
    foreach (Animal animal in zoo)
    {
      Console.WriteLine(animal.Name);
    }
    Console.ReadKey();
  }

2. Coprogramming in unity

2.1. Introduction of process thread program

Process:

  • The process has its own independent heap and stack, which neither shares the heap nor the stack. The process is scheduled by the operating system.
  • A process is a running activity of a program with independent functions on a data set.

Thread:

  • Thread has its own independent stack and shared heap, shared heap, not shared stack. Threads are scheduled by the operating system.
  • A process can contain multiple threads.
  • Threads run asynchronously, and multiple threads in a multiprocessor can run simultaneously.

Program:

  • The coroutines share the heap and do not share the stack. The programmer displays the scheduling in the code of the coroutine.
  • The execution of the coroutine is on the main thread of the game. Only one coroutine can be executed at a time.
  • Threads avoid meaningless scheduling and improve performance, but it requires programmers to take responsibility for scheduling.

2.2. Use of cooperative program

Unity’s coprocessor system is a simple and powerful interface based on C, ienumerator.

The above is the whole content of this article, I hope to help you in your study, and I hope you can support developeppaer more.

Recommended Today

Be a node crawler from the perspective of a prostitute

Story background Today, I originally wanted to write a webpack blog. However, I was worried that I could not explain some details clearly. In line with the principle of responsibility, I wanted to review and write on the Internet Then I found a website of webpack, but when I was reading it with relish, a […]