Sunday 25 May 2014

Using PTaskScheduler - UniParallel Unity package


UniParallel is a .Net package designed to work with Unity and help developers to easily utilize the power of multi-core devices.

The PTaskScheduler class allows simple and easy scheduling of background tasks in a variety of ways.

There are 4 types of background tasks, each designed to behave in a very specific way


  • Use ImmediateTask if you want to schedule a task for immediate execution
  • Use TimedTask if you want to schedule a task for delayed execution
  • Use RepeatableTask to execute tasks a certain number of times on a interval
  • And use InfiniteTask  to execute tasks indefinitely on a certain interval

All scheduled tasks are executed without blocking the calling thread. While this can help increase parallelism in your app or game, it also means that you'll have to what for synchronization and concurrency problems.
When using this library in your Unity application, don't directly modify resources that are owner by the rendering thread.

You can and should, however, use this library for processing of any independent data processing, like loading of assets, processing object specific calculation, downloading files of the internet, so on and so forth.

PTaskScheduler is extremely light weight and designed to work without impacting the application using it.

Code examples


Scheduling tasks depends on 2 components: The task processor PTaskScheduler and a task. All tasks implement the IUniTask interface.

Let's start with the most simple task type: ImmediateTask

 [...]
 PTaskScheduler   taskScheduler = PTaskScheduler.Create();  
 IUniTask task = new ImmediateTask(ImmediateTaskProc);   
 taskScheduler.Enqueue(task);
 [...]

 private void ImmediateTaskProc()  
 {  
    System.Console.WriteLine("Hello from another thread");  
 }  


To schedule method to be executed after a certain delay, use  TimedTask
 [...]
 PTaskScheduler   taskScheduler = PTaskScheduler.Create();  
 int delayInMilliseconds=1000;
 IUniTask task = new TimedTask(DelayedTaskProc, delayInMilliseconds);   
 taskScheduler.Enqueue(task);
 [...]

 private void DelayedTaskProc()  
 {  
    System.Console.WriteLine("Hello after 1000 milliseconds");  
 }  


To schedule method to be executed repeatedly on interval, use  RepeatableTask
 [...]
 PTaskScheduler   taskScheduler = PTaskScheduler.Create();  
 int delayInMilliseconds=1000;
 int runCount = 10;
 IUniTask task = new RepeatableTask(RepeatableTaskProc, delayInMilliseconds, runCount);   
 taskScheduler.Enqueue(task);
 [...]

 private void RepeatableTaskProc()  
 {  
    System.Console.WriteLine("This will be executed 10 times every 1000 milliseconds");  
 }  


And if you want to schedule method to be executed indefinitely on interval, use  InfiniteTask. You can cancel the execution of any task by calling the method Cancel() on the task object in question
 [...]
 PTaskScheduler   taskScheduler = PTaskScheduler.Create();  
 int delayInMilliseconds=1000;
 IUniTask task = new InfiniteTask(InfiniteTaskProc, delayInMilliseconds);   
 taskScheduler.Enqueue(task);

 [...]
 // OK, time to shutdown
 task.Cancel(); 
 [...]

 private void InfiniteTaskProc()  
 {  
    System.Console.WriteLine("This will be executed indefinitely every 1000 milliseconds");  
 }  

Naturally, you can combine all the different task types described about and enqueue them in the same scheduler.

You can also schedule execute methods that have arguments using Lambda Expressions.
The example below uses a ImmediateTask but the same strategy applies to any task type.
 [...]
 PTaskScheduler   taskScheduler = PTaskScheduler.Create();  
 int delayInMilliseconds=1000;
 IUniTask task = new ImmediateTask(
            ()=>WithArgs(" text argument ", 42)
                        );
 taskScheduler.Enqueue(task);
 [...]
 private void WithArgs(string strArg, int intArg)
 {
   Debug.Log("string arg = "+strArg+" int arg "+intArg);
 }


This library is being actively used in many platforms that support .Net or Mono, including all supported by Unity.

If you have any questions or suggestions, please send a message to the gmail account junity3.