C# core knowledge review - 13. Multi-threading, preprocessor instructions

1.Multi-threading

Before understanding threads, you must first understand processes
. A process is a program in a computer that performs a running activity on a certain data set. It is the
basic unit of resource allocation and scheduling in the system and the basis of the operating system structure.
In human terms: Open an application. A process is started on the slow work system.
The processes can run independently of each other without interfering with each other.
The processes can also access each other and operate.

What is a thread?
The smallest unit that an operating system can perform computing scheduling.
It is included in the process and is the actual operating unit in the process
- a thread refers to a single sequential control flow in the process. Multiple threads can be concurrently used in a process. The
programs we currently write are all in the main thread.

A simple understanding of threads:
it is a "pipeline" in which code runs from top to bottom.

What is multithreading?
We can start new threads through code.
Multiple "pipes" that can run code at the same time are called multithreading.

public class test : MonoBehaviour
{
    static bool isrun = true;
    private void Start()
    {
        //语法相关
        //线程类Thread
        //需要引用命名空间using System.Threading;
        //1.申明一个新的线程
        !!!注意线程执行的代码需要封装到一个函数中!!!/
        Thread t = new Thread(NewThread);

        //2.启动线程
        t.Start();

        //3.设置为后台线程
        //当前台线程都结束了的时候,整个程序也就结束了,即使还有后台线程正在运行
        //后台线程不会防止应用程序的进程被终止掉
        // 如果不设置为后台线程可能导致进程无法正常关闭
        t.IsBackground = true;

        // 4.关闭释放一个线程
        //如果开启的线程中不是死循环是能够结束的逻辑那么不用刻意的去关闭它
        //如果是死循环想要中止这个线程有两种方式
        //4.1-死循环中boo1标识
        isrun = false;

        //4.2-通过线程提供的方法(注意在.Net core版本中无法中止会报错)
        try
        {
            t.Abort();
        }
        catch { }

        //5.线程休眠
        //让线程休眠多少毫秒
        Thread.Sleep(500);
    }
    static void NewThread()
    {
        //新开线程 执行代码逻辑
        Debug.Log("start");
        while (isrun) {
            Debug.Log(0);
        }
    }
}

Data is shared between threads.
The memory used by multiple threads is shared and belongs to the application (process).
Therefore, be aware that problems may occur when multiple threads operate the same memory area at the same time.
You can avoid problems by locking
lock( reference type object)

public class test : MonoBehaviour
{
    static bool isrun = true;
    static object obj = new object();
    private void Start()
    {        
        Thread t = new Thread(NewThread);
        t.Start();
        t.IsBackground = true;
        isrun = false;
        while (true)
        {
            lock (obj)
            {
                Debug.Log("100");
            }
        }
    }
    static void NewThread()
    {
        //新开线程 执行代码逻辑
        while (isrun) {
            lock (obj)
            {
                Debug.Log(0);
            }            
        }
    }
}

Use multi-threading to specifically handle some complex and time-consuming logic

Purpose: pathfinding, network communication.

2. Preprocessor directives

//定义一个符号
#define Unity4
#define Unity5
#define Unity2017
#define Unity2019
//取消定义一个符号
#undef Unity4

#define IOs
#define Android
#define PC

using System;
using UnityEngine;

public class test : MonoBehaviour
{
    private void Start()
    {
        #region 知识点一什么是编译器
        //编译器是一种翻译程序
        //它用于将源语言程序翻译为目标语言程序
        //源语言程序:某种程序设计语言写成的,比如C#、C、C++、Java等语言写的程序
        //目标语言程序:二进制数表示的伪机器代码写的程序
        #endregion
        #region 知识点二什么是预处理器指令
        //预处理器指令指导编译器在实际编译开始之前对信息进行预处理
        //预处理器指令都是以#开始
        //预处理器指令不是语句,所以它们不以分号;结束
        //目前我们经常用到的折叠代码块就是预处理器指令
        #endregion
        #region 知识点三常见的预处理器指令
        // 1
        //#define
        //定义一个符号,类似个没有值的变量
        //#undef
        //取消define定义的符号,让其失效
        //两者都是写在脚本文件最前面
        //一股配合if指令使用或配合特性

        // 2
        //#if
        //#elif
        //#else
        //#endif
        //和if语句规则一样,一般配合#define定义的符号使用
        //用于告诉编译器进行编译代码的流程控制
        //如果发现有Unity4这个符号那么其中包含的代码就会被编译器翻译
        //可以通过逻辑或和逻辑与进行多种符号的组合判断

      #if Unity4
        console.Write Line("版本为Unity4");
      # elif Unity2017&&I0S
        Console.Write Line("版本为unity2017");
        //#warning这个版本不合法
        //#error这个版本不准执行
      #else
         Debug.Log("其它版本");
      #endif
        // 3
        //#warning
        //#error        
        //告诉编译器
        //是报警告还是报错误
        //一般还是配合if使用
        #endregion
    }
}

 

Guess you like

Origin blog.csdn.net/qq_29296473/article/details/131643787