//--------------------------------------- UNITY MONOBEHAVIOUR COMMANDS -------------------------------------- //--------------------------------------- UNITY MONOBEHAVIOUR COMMANDS -------------------------------------- //--------------------------------------- UNITY COROUTINE & PROVIDER-THREAD IMPLEMENTATION -------------------------------------- //--------------------------------------- UNITY COROUTINE & PROVIDER-THREAD IMPLEMENTATION -------------------------------------- #region UNITY COROUTINE & PROVIDER-THREAD IMPLEMENTATION /// <summary> /// Unlike "StartMultithreadedWorkloadExecution", you will have to build your own IThreadWorkerObject. /// Downside: It requires some extra work. Upside: you got more controll over what goes in and comes out /// Infact: You can create you own polymorphed IThreadWorkerObject-array, each ellement being a completely different type. For example: the statemachines of enemies are IThreadWorkerObject's and the array contains completely different classes with enemies/AI-behaviours. /// </summary> /// <param name="workerObjects">An array of IThreadWorkerObject objects to be handled by the threads. If you want multiple cores/threads to be active, make sure that the number of IThreadWorkerObject's proves matches/exeeds your preferred number maxWorkingThreads. </param> /// <param name="onComplete">Fired when all re-packaged workLoad-objects are finished computing</param> /// <param name="onPackageExecuted">Fires foreach finished re-packaged set of workLoad-object</param> /// <param name="maxThreads"> Lets you choose how many threads will be run simultaneously by the threadpool. Default: -1 == number of cores minus one, to make sure the MainThread has at least one core to run on. (quadcore == 1 core Mainthread, 3 cores used by the ThreadPoolScheduler)</param> /// <param name="scheduler">If Null, a new ThreadPoolScheduler will be instantiated.</param> /// <param name="safeMode">Executes all the computations within try-catch events, logging it the message + stacktrace</param> public void StartASyncThreads(IThreadWorkerObject[] workerObjects, ThreadPoolSchedulerEvent onCompleteCallBack, ThreadedWorkCompleteEvent onPackageExecuted = null, int maxThreads = -1, bool safeMode = true) { MainThreadWatchdog.Init(); MainThreadDispatcher.Init(); Debug.Log("ExecuteThreadedWork " + workerObjects.Length); if (_shedularBusy) { Debug.LogError("You are trying the start a new ASync threading-process, but is still Busy!"); return; } if (workerObjects == null || workerObjects.Length == 0) { Debug.LogError("Please provide an Array with atleast \"IThreadWorkerObject\"-object!"); return; } if (!ForceToMainThread) { _isAborted = false; this.onCompleteCallBack = onCompleteCallBack; this.onWorkerObjectDoneCallBack = onPackageExecuted; _shedularBusy = true; _providerThreadBusy = true; StartCoroutine("WaitForCompletion"); //--------------- Start Waiting for the Provider-thread to complete -------------------- workData = new ASyncThreadWorkData(workerObjects, safeMode, maxThreads); Thread providerThread = new Thread(new ThreadStart(InvokeASyncThreadPoolWork)); providerThread.Start(); //--------------- Start Waiting for the Provider-thread to complete -------------------- } else { //--------------- Execute all work in one bunch! -------------------- StartCoroutine(WaitAndExecuteWorkerObjects(workerObjects)); //--------------- Execute all work in one bunch! -------------------- } }
public void StartASyncThreads(IThreadWorkerObject[] workerObjects, ThreadPoolSchedulerEvent onCompleteCallBack, ThreadedWorkCompleteEvent onPackageExecuted = null, int maxThreads = -1, bool safeMode = true) { bool shedularBusy = this._shedularBusy; if (shedularBusy) { Debug.LogError("You are trying the start a new ASync threading-process, but is still Busy!"); } else { bool flag = workerObjects == null || workerObjects.Length == 0; if (flag) { Debug.LogError("Please provide an Array with atleast \"IThreadWorkerObject\"-object!"); } else { this._isAborted = false; this._shedularBusy = true; this._providerThreadBusy = true; this.onCompleteCallBack = onCompleteCallBack; this.onWorkerObjectDoneCallBack = onPackageExecuted; bool flag2 = !this.ForceToMainThread; if (flag2) { base.StartCoroutine("WaitForCompletion"); this.workData = new ASyncThreadWorkData(workerObjects, safeMode, maxThreads); this.providerThread = new Thread(new ThreadStart(this.InvokeASyncThreadPoolWork)); this.providerThread.Start(); } else { base.StartCoroutine(this.WaitAndExecuteWorkerObjects(workerObjects)); } } } }
//--------------------------------------- 4 MULTI THREADED WORK EXECUTION OVERLOADS -------------------------------------- //--------------------------------------- 4 MULTI THREADED WORK EXECUTION OVERLOADS -------------------------------------- //--------------------------------------- THREAD POOL SCHEDULAR -------------------------------------- //--------------------------------------- THREAD POOL SCHEDULAR -------------------------------------- #region THREAD POOL SCHEDULAR /// <summary> /// Unlike "StartMultithreadedWorkloadExecution", you will have to build your own IThreadWorkerObject. /// Downside: It requires some extra work. Upside: you got more controll over what goes in and comes out /// Infact: You can create you own polymorphed IThreadWorkerObject-array, each ellement being a completely different type. For example: the statemachines of enemies are IThreadWorkerObject's and the array contains completely different classes with enemies/AI-behaviours. /// </summary> /// <param name="workerObjects">An array of IThreadWorkerObject objects to be handled by the threads. If you want multiple cores/threads to be active, make sure that the number of IThreadWorkerObject's proves matches/exeeds your preferred number maxWorkingThreads. </param> /// <param name="onComplete">Fired when all re-packaged workLoad-objects are finished computing</param> /// <param name="onPackageExecuted">Fires foreach finished re-packaged set of workLoad-object</param> /// <param name="maxThreads"> Lets you choose how many threads will be run simultaneously by the threadpool. Default: -1 == number of cores minus one, to make sure the MainThread has at least one Core to run on. (quadcore == 1 Core Mainthread, 3 cores used by the ThreadPoolScheduler)</param> /// <param name="scheduler">If Null, a new ThreadPoolScheduler will be instantiated.</param> /// <param name="safeMode">Executes all the computations within try-catch events, logging it the message + stacktrace</param> /// <returns>A ThreadPoolScheduler that handles all the repackaged workLoad-Objects</returns> public static ThreadPoolScheduler StartMultithreadedWorkerObjects(IThreadWorkerObject[] workerObjects, ThreadPoolSchedulerEvent onCompleteCallBack, ThreadedWorkCompleteEvent onPackageExecuted = null, int maxThreads = -1, ThreadPoolScheduler scheduler = null, bool safeMode = true) { if (scheduler == null) { scheduler = CreateThreadPoolScheduler(); } scheduler.StartASyncThreads(workerObjects, onCompleteCallBack, onPackageExecuted, maxThreads, safeMode); return(scheduler); }
/// <summary> /// Unlike "StartMultithreadedWorkloadExecution", you will have to build your own IThreadWorkerObject. /// Downside: It requires some extra work. Upside: you got more controll over what goes in and comes out /// Infact: You can create you own polymorphed IThreadWorkerObject-array, each ellement being a completely different type. For example: the statemachines of enemies are IThreadWorkerObject's and the array contains completely different classes with enemies/AI-behaviours. /// </summary> /// <param name="workerObjects">An array of IThreadWorkerObject objects to be handled by the threads. If you want multiple cores/threads to be active, make sure that the number of IThreadWorkerObject's proves matches/exeeds your preferred number maxWorkingThreads. </param> /// <param name="onComplete">Fired when all re-packaged workLoad-objects are finished computing</param> /// <param name="onPackageExecuted">Fires foreach finished re-packaged set of workLoad-object</param> /// <param name="maxThreads"> Lets you choose how many threads will be run simultaneously by the threadpool. Default: -1 == number of cores minus one, to make sure the MainThread has at least one core to run on. (quadcore == 1 core Mainthread, 3 cores used by the ThreadPoolScheduler)</param> /// <param name="scheduler">If Null, a new ThreadPoolScheduler will be instantiated.</param> /// <param name="safeMode">Executes all the computations within try-catch events, logging it the message + stacktrace</param> public void StartASyncThreads(IThreadWorkerObject[] workerObjects, ThreadPoolSchedulerEvent onCompleteCallBack, ThreadedWorkCompleteEvent onPackageExecuted = null, int maxThreads = -1, bool safeMode = true) { MainThreadWatchdog.Init(); MainThreadDispatcher.Init(); if (_shedularBusy) { Debug.LogError("You are trying the start a new ASync threading-process, but is still Busy!"); return; } if (workerObjects == null || workerObjects.Length == 0) { Debug.LogError("Please provide an Array with atleast \"IThreadWorkerObject\"-object!"); return; } if (!ForceToMainThread) { _isAborted = false; this.onCompleteCallBack = onCompleteCallBack; this.onWorkerObjectDoneCallBack = onPackageExecuted; _shedularBusy = true; _providerThreadBusy = true; StartCoroutine("WaitForCompletion"); //--------------- Start Waiting for the Provider-thread to complete -------------------- workData = new ASyncThreadWorkData(workerObjects, safeMode, maxThreads); Thread providerThread = new Thread(new ThreadStart(InvokeASyncThreadPoolWork)); providerThread.Start(); //--------------- Start Waiting for the Provider-thread to complete -------------------- } else { //--------------- Execute all work in one bunch! -------------------- StartCoroutine(WaitAndExecuteWorkerObjects(workerObjects)); //--------------- Execute all work in one bunch! -------------------- } }