Unity多线程编程的一点心得

本文介绍了在Unity中使用多线程的方法,包括ThreadJob类的设计,用于启动和监控线程的生命周期,以及ThreadManager类,用于子线程与主线程间的通信。ThreadJob类利用原子操作确保线程状态的安全更新,ThreadManager类则通过队列和锁机制确保回调在主线程中正确执行。此外,还给出了具体的使用示例和注意事项。
摘要由CSDN通过智能技术生成

一共有两个类。第一个类ThreadJob如下:

using System.Collections;using System.Collections.Generic;using UnityEngine;using System;using System.Threading;public class ThreadJob {

    public bool isDone { get {

            int val = 1;

            Interlocked.CompareExchange(ref val,0, _isDone);

            if (val == 0)

                return true;

            return false;

        }

        set {

            _isDone = value ? 1 : 0;

        }

    }

    private int _isDone;

    protected Thread thread;

 

    public void Start() {

        thread = new Thread(Run);

        thread.IsBackground = true;

        thread.Start();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值