Created
December 4, 2022 04:20
-
-
Save EricHu33/940b3ce90843f330404b9956f14a3a34 to your computer and use it in GitHub Desktop.
Example of using async with job system
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Threading.Tasks; | |
using UnityEngine; | |
using Unity.Jobs; | |
using Unity.Collections; | |
using Unity.Profiling; | |
struct NumberIncrementJob : IJob | |
{ | |
public NativeArray<float> Numbers; | |
public ProfilerMarker Marker; | |
public void Execute() | |
{ | |
Marker.Begin(); | |
for (int i = 0; i < Numbers.Length; i++) | |
{ | |
Numbers[i] += 10; | |
} | |
Marker.End(); | |
} | |
} | |
public class AsyncJobController : MonoBehaviour | |
{ | |
static readonly ProfilerMarker m_profilerMarker = new ProfilerMarker("Incre job workload"); | |
static readonly ProfilerMarker m_profilerAsyncEndMarker = new ProfilerMarker("Async End Marker"); | |
private NativeArray<float> m_numbers; | |
private JobHandle m_jobHandle; | |
private void OnEnable() | |
{ | |
m_numbers = new NativeArray<float>(200000, Allocator.Persistent); | |
for (int i = 0; i < m_numbers.Length; i++) | |
{ | |
m_numbers[i] += 0; | |
} | |
} | |
private void OnDisable() | |
{ | |
m_numbers.Dispose(); | |
} | |
private IEnumerator CheckJobHandleComplete(TaskCompletionSource<bool> tcs, int frameDelay) | |
{ | |
for (int i = 0; i < frameDelay; i++) | |
{ | |
yield return new WaitForEndOfFrame(); | |
} | |
tcs.SetResult(true); | |
} | |
private void PrintAllElementValues() | |
{ | |
for (int i = 0; i < m_numbers.Length; i++) | |
{ | |
Debug.Log("element at {" + i + "} is now " + m_numbers[i]); | |
} | |
} | |
//right click on inspector to invoke this function | |
[ContextMenu("test async")] | |
private async void AsyncJobPerformer() | |
{ | |
. | |
//We use TaskCompleteSource as a *signal* indicat whether jobHandle's status become complete or not. | |
//then await the tcs.Task like awaiting a task. | |
var job1Tcs = new TaskCompletionSource<bool>(); | |
var numberIncrementJob = new NumberIncrementJob() | |
{ | |
Numbers = m_numbers, | |
Marker = m_profilerMarker, | |
}; | |
m_jobHandle = numberIncrementJob.Schedule(m_jobHandle); | |
//wait at least 3 frames to invoke handle's Complete() | |
var checkJob = StartCoroutine(CheckJobHandleComplete(job1Tcs, 3)); | |
//another task as example, not important, just showcase how multiple task can be awaiting together | |
var delayTask = Task.Delay(1); | |
//this line make sure all tasks(Job1Tcs, delay task has been done) | |
await Task.WhenAll(delayTask, job1Tcs.Task); | |
m_jobHandle.Complete(); | |
// we can safely access native array now | |
// PrintAllElementValues(); | |
//first part of the job done, done the 2nd part | |
var job2Tcs = new TaskCompletionSource<bool>(); | |
m_jobHandle = numberIncrementJob.Schedule(m_jobHandle); | |
var checkJob2 = StartCoroutine(CheckJobHandleComplete(job2Tcs, 3)); | |
var delayTask2 = Task.Delay(1); | |
await Task.WhenAll(delayTask2, job2Tcs.Task); | |
m_jobHandle.Complete(); | |
// we can safely access native array now | |
//PrintAllElementValues(); | |
m_profilerAsyncEndMarker.Begin(); | |
Debug.LogError("pause to check profiler"); | |
m_profilerAsyncEndMarker.End(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In this screenshot, you can see the job was executing in worker thread at previous frame time. while the async end maker is backing to mainthread on current frame.