#ThreadSave Stack
1 messages · Page 1 of 1 (latest)
public struct Stack32
{
public NativeArray<int> stack;
public int size;
public int count;
public Stack32(int size)
{
this.size = size;
this.count = 0;
stack = new NativeArray<int>(size, Allocator.Persistent);
}
public void Fill()
{
for (int i = 0; i < size; i++)
{
Push(i);
}
}
public void Push(int i)
{
stack[count] = i;
count++;
}
public int Pop()
{
count--;
return stack[count];
}
public NativeArray<int> PopRange(int ammount)
{
NativeArray<int> memory=new NativeArray<int>(count, Allocator.Temp);
for(int i=0; i < ammount; i++)
{
memory[i] = Pop();
}
return memory;
}
public void Destruct()
{
stack.Dispose();
}
}
I need help making my stack ThreadSave
thank you but I don t understand that code
making stuff threadsafe is hard
this seems to be using unsafe code, which I am not
Im not familiar with unsafe code yet
do you only need to be able to read from it in multiple threads and read the whole thing?
then just send it into a job and copy it and then pop from the copy
I'd need to pop and push to it from multiple times
writing and reading at the same time isnt threadsafe for any of the native containers (except NativeStream in some circumstances)
Then what does threadsave mean, I thought you'd just have to lock the Stack32 struct when pushing or reading from it
locking is a bit different
that forces all the threads trying to access something to wait while another thread is accessing it
so it's making everything very slow right?
yup
i've not tried to use locks with jobs or native containers, it is against the design philosophy
so ur saying that without using nativecontainers it would be easyer to make it threadsave?