Unity's C# Job System
Tags: unity3d, gamedev, csharp, todayilearned
Published: 2021-05-25 04:54:42

<div class="modal-body">
<p>There are times when you need to take full advantage of every ounce of performance possible to make your dream game a reality. Enter the Job System in Unity. It allows the developer to write multithreaded code that can still interact with the rest of&nbsp;Unity.</p>
<h3>What is multithreading?</h3>
<p>Multithreading allows the program to run faster by taking advantage of the CPU’s ability to process many threads at the same time across multiple cores. Typically this means that a main thread spawns several other threads that in turn performs work. The problem with this is that games tend to spawn a ton of threads. So much so that the overhead causes the CPU to run less efficiently. So how do we solve this issue? You get a&nbsp;Job.</p>
<h3>What is a&nbsp;Job?</h3>
<p>A Job system is a group of <a href="https://docs.microsoft.com/en-us/cpp/parallel/multithreading-creating-worker-threads?view=msvc-160">worker threads</a> that are being executed across multiple cores of the CPU. This allows you to save resources from having to perform context switching. The Job system operates on a Job queue where a worker thread takes an item from the queue and executes it. It does all of this while making sure to execute this in the appropriate order so that issues don’t arise. Not all Jobs have dependencies, while others do so throughput can depend on the input and dependencies associated with them. The best part? Unity’s C# Job system detects all potential race conditions and protects you from them. How sweet is&nbsp;that?!</p>
<h3>Simple Jobs&nbsp;Example</h3>
<p>To create a job in Unity, you need to implement the <a href="https://docs.unity3d.com/ScriptReference/Unity.Jobs.IJob.html">IJob</a> interface. This interface allows you to define a struct that has parameters and an execute function. In the example below, we define a job that adds two numbers together and returns the result in an&nbsp;array.</p>
<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*iRysOSxoggUHUsae"></figure><p>Once you have the job, you need to schedule it to run in the main thread. Notice how we allocate space for the result array and then destroy it after we are done with&nbsp;it.</p>
<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*SJI30GsmsUBh2mQD"></figure><p>While this might seem like a very rudimentary example of Jobs, there is actually a lot you can do with them and they do provide an extreme speed benefit! <a href="https://www.youtube.com/watch?v=kwnb9Clh2Is">Watch this video from GDC 2018 for&nbsp;more</a></p>

<iframe width="560" height="315" src="https://www.youtube.com/embed/kwnb9Clh2Is" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>

<p><a href="https://twitter.com/maurerpwer"><strong>Tweet me</strong></a><strong> if you’ve ever used Jobs before and what you did with&nbsp;them!</strong></p>
<img src="https://medium.com/_/stat?event=post.clientViewed&amp;referrerSource=full_rss&amp;postId=535364596b4a" width="1" height="1" alt="">
</div>