Quick answer: Pre-size NativeList capacity. In job: list.AsParallelWriter().AddNoResize(item). Resize main-thread between jobs.
IJobParallelFor appends results to a shared NativeList. Throws because resize isn't thread-safe.
The Fix
var results = new NativeList<int>(Allocator.TempJob);
results.SetCapacity(maxExpected); // pre-size
struct FilterJob : IJobParallelFor {
[ReadOnly] public NativeArray<int> In;
public NativeList<int>.ParallelWriter Out;
public void Execute(int i) {
if (In[i] > 10) Out.AddNoResize(In[i]);
}
}
var handle = new FilterJob { In = data, Out = results.AsParallelWriter() }.Schedule(data.Length, 64);
handle.Complete();
Pre-sized capacity + AddNoResize is lock-free. List length grows as appends succeed.
Verifying
Job completes. results.Length matches expected. Without ParallelWriter: throw on parallel add.
“Pre-size. ParallelWriter. Lock-free.”
Related Issues
For NativeArray dispose, see dispose. For ReadOnly write, see readonly.
Pre-size. ParallelWriter. Append.