Can I write values to a Transform via main thread and jobs simultaneously?

@Joachim_Ante_1

I read your answer in this thread and have a further question.

Is it thread safe when I write to a Transform via Update() on the main thread and Execute() in jobs simultaneously or should I move code in Update to a new job?

Transform cubeTransform;

void Update()
{
    cubeTransform.position = new Vector3(0,0,0);
}

// ...

public struct TransformJob : IJobParallelForTransform
{
    // transform is passed from cubeTransform     
    public void Execute(int index, TransformAccess transform)
    {
        transform.position = new Vector3(1,1,1);
    }
}

Its safe, but you shouldn’t. Because that will cause jobs to complete & main thread will wait until their completion.

Anything “unsafe” with jobs is usually caught by safety system.

Better solution is to run main thread logic when jobs aren’t running.
E.g. when jobs are complete and new ones aren’t scheduled yet. Its fairly simple to implement with Entities, harder with MonoBehaviours a bit, cause you’d have to control execution order manually.

1 Like

@Joachim_Ante_1

Even if I write a position rather than grabbing to a single transform on the main thread(a MonoBehaviour), does it also wait for all jobs against that hierarchy to complete?

I’m confused as to whether it’s safe to write to a transform on the main thread and jobs competitively.
All the documents and forum threads say it’s safe to access among jobs, but I don’t know if it’s safe to access it from the main thread simultaneously either.

Would you mind telling me “it’s okay” or “don’t do that accessing from the main thread and jobs together”?