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!

user1514077
  • 61
  • 1
  • 8
  • 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? – chris Sep 21 '12 at 12:26
  • A solution to your problem: [link](http://stackoverflow.com/questions/1813557/c-sharp-list-concurrent-removing-and-adding) – Artless Sep 21 '12 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 – user1514077 Sep 21 '12 at 12:36
  • @Trickery i have seen that link , what i don't get is assignment of arrays would also need a lock? – user1514077 Sep 21 '12 at 12:37

2 Answers2

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);
}
chris
  • 403
  • 3
  • 10
  • 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 ? – user1514077 Sep 21 '12 at 14:05
  • `public static` declaration is probably a good idea, but you would still need to lock during the `.Add()` stage. See [this article](http://blogs.technet.com/b/efleis/archive/2006/06/01/431698.aspx) – chris Sep 21 '12 at 16:48
  • 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 ! – user1514077 Sep 24 '12 at 09:30
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.

Artless
  • 4,522
  • 1
  • 25
  • 40