I’m getting InvalidOperationException: The native container has been declared as [ReadOnly] in the job, but you are writing to it.
This happens when i try to dequeue from the NativeQueue inside an job.
struct MyJob : IJobParallelFor {
[ReadOnly]
public NativeQueue<int> MyNativeQueue;
public void Execute(int index) {
while (MyNativeQueue.TryDequeue(out var element)) {
// Do something with the element
}
}
}
Yes i know but if i removed the [ReadOnly] i can only use the concurrent version and that only allows me to “Enqueue”.
I will get an error
InvalidOperationException: MyJob.NativeQueue is not declared [ReadOnly] in a IJobParallelFor job. The container does not support parallel writing. Please use a more suitable container type.
So i guess i can only read the the values of the NativeQueue in the main thread.
use IJob instead of IJobParallelFor. If you need something to do inside IJobParallelFor you have to use concurrent version. Right now for NativeQueue with concurrent you can only use enqueue. Maybe in future we can dequeu
You are not able to read from a NativeQueue in parallel.
What you could do is schedule an IJob and in it dequeue the elements from the NativeQueue into a NativeArray or NativeList. After which you are able to read from the NativeArray in your IJobParallelFor.