0

I have an array list which is continuously updated every second. I have to use the same array list in two other threads and make local copies of it. I have done all of this but i get weird exceptions of index out of bound , What i have found out so far is that i have to ensure some synchronization mechanism for the array list to be used across multiple threads.

this is how i am making it synchronized:

for (int i = 0; i < Globls.iterationCount; i++)
            {
                if (bw_Obj.CancellationPending)
                {
                    eve.Cancel = true;
                    break;
                }

                byte[] rawData4 = DMM4.IO.Read(4 * numReadings);
                TempDisplayData_DMM4.Add(rawData4);
                Globls.Display_DataDMM4 = ArrayList.Synchronized(TempDisplayData_DMM4);
                Globls.Write_DataDMM4 = ArrayList.Synchronized(TempDisplayData_DMM4);

            }

in other thread i do the following to make local copies:

ArrayList Local_Write_DMM4 = new ArrayList();
Local_Write_DMM4 = new ArrayList(Globls.Write_DataDMM4); 

Am i synchronizing the arraylist in right way?, also do i need to lock while copying array-list as well:

 lock (Globls.Display_DataDMM4.SyncRoot){Local_Temp_Display1 = new ArrayList(Globls.Display_DataDMM4);}

or for single operations its safe?. I haven't actually ran this code i need to run it over the weekend and i don't want to see another exception :(. please help me on this!

4
  • Can you explain what you are aiming for? Avoiding the exception is secondary to that. Are you worried about the two threads containing lists with different contents? Or worried that one thread may change the list items while the other is reading? Commented Sep 21, 2012 at 12:26
  • A solution to your problem: link Commented Sep 21, 2012 at 12:27
  • @chris the arraylist never has different contents. i am trying to avoid exception while copying Local_Write_DMM4 = new ArrayList(Globls.Write_DataDMM4); and i used the above approach, i have asked two questions which are my major concern Commented Sep 21, 2012 at 12:36
  • @Trickery i have seen that link , what i don't get is assignment of arrays would also need a lock? Commented Sep 21, 2012 at 12:37

2 Answers 2

2

as @Trickery stated assignment needs to be locked since the source array Globls.Write_DataDMM4 can be modified by another thread during enumeration. It is essential therefore to lock both when populating the original array and when making your copy

for (int i = 0; i < Globls.iterationCount; i++)
{
    if (bw_Obj.CancellationPending)
    {
        eve.Cancel = true;
        break;
    }

    byte[] rawData4 = DMM4.IO.Read(4 * numReadings);
    TempDisplayData_DMM4.Add(rawData4);

    lock (Globls.Display_DataDMM4.SyncRoot)
    {
    Globls.Write_DataDMM4 = ArrayList.Synchronized(TempDisplayData_DMM4);
    }

}

and

lock (Globls.Display_DataDMM4.SyncRoot)
{
     Local_Temp_Display1 = new ArrayList(Globls.Display_DataDMM4);
}
Sign up to request clarification or add additional context in comments.

3 Comments

what if i don't do the assignment :Globls.Write_DataDMM4 = ArrayList.Synchronized(TempDisplayData_DMM4); instead i initialize public static ArrayList Write_DataDMM4 = ArrayList.Synchronized(new ArrayList()); and i just add to the list Globls.Display_DataDMM4.Add(rawData); , i have synchronized arraylist to start with that i am adding data in , do in need to lock it as well ?
public static declaration is probably a good idea, but you would still need to lock during the .Add() stage. See this article
Well the article mentions that doing add/remove do not corrupt the list, i ran a 70 hours test without locking add, it didn't cause any problem, thanks !
1

Yes, all operations on your ArrayList need to use Lock.

EDIT: Sorry, the site won't let me add a comment to your question for some reason.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.