Sunday, June 26, 2022
HomeGame Developmentmultithreading - Find out how to use Unity job system to do...

multithreading – Find out how to use Unity job system to do background work spanning a number of frames?


I’ve a gradual stage era algorithm that freezes the principle thread when it runs.
I discovered this publish which is strictly the identical drawback, however predates the Jobs system.

All the roles examples I discovered are good for parallelizing a bunch of cases of some operate however the JobHandle.Full() nonetheless pauses the principle loop till they’re completed. Is there a approach to offload a operate that can take a number of frames to finish with Jobs?

Or am I higher off making a thread manually as instructed in that different publish?

Thanks

Edit

In response to DMGregory’s remark, I see I need not name Full() for it to run.

This code appears to work, is it protected?


public class JobTest : MonoBehaviour
{
    JobHandle myJob;
    bool began = false;
    bool completed = false;
    void Replace()
    {
        if (!began)
        {
            if (Time.realtimeSinceStartup > 5)
            {
                print("scheduling");
                myJob = scheduleLongJob();
                JobHandle.ScheduleBatchedJobs(); 
                began = true;
            }
        }

        if (myJob.IsCompleted && began)
        {
            print("job accomplished");
            completed = true;
        }        
    }
    JobHandle scheduleLongJob()
    {
        LongJob myJob = new LongJob();
        return myJob.Schedule(); 
    }
}

public struct LongJob : IJob
{
    public void Execute()
    {
        float u = 0;
        for (int i = 0; i < 50000000; i++)
        {
            u += math.exp10(math.sqrt(10f));
        }
        Debug.Log(u);
    }
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments