So I’m seeing fun stuff playing with threads.
I have a method that starts 2 threads and goes on with its life and I tried doing the following to keep things safe.
bool CopyOk1 = false;
bool CopyOk2 = false;
new Thread(() => FirstMethod(tempList1, ref CopyOk1)).Start();
new Thread(() => SecondMethod(tempList2, ref CopyOk2)).Start();
var spinner = new SpinWait();
while (!CopyOk1 || !CopyOk2)
spinner.SpinOnce();
then in both methods I start with the following
private static void FirstMethod(List<T> templist, ref bool CopyOk)
{
var temp = new T[templist.Count];
templist.CopyTo(temp);
CopyOk = true;
//moves on
}
My intention here is to copy the passed list so I can change and use it safely inside the threads ASAP to unblock the caller thread.
My problem is that on the second method unpredictably, between the array initialization and the list CopyTo the base list changes, somehow, by something.
These lists are created inside the caller thread and are not actually used after the threads are started so I have absolutely NO idea how/why this is happening.
No doubt I’m doing something wrong here, but my noobish skills won’t let me see, any help is appreciated.
Nothing in the code that you posted suggests that you are doing anything wrong. The problem has to lie elsewhere- most likely, as commenters have suggested, in the code that populates List1/List2. If you are threading that as well, perhaps you are not waiting for that thread to finish populating the lists before proceeding?