views:

85

answers:

1

why this lock test doesn't work ? it's throwing an exception bellow Console.Write that collection was modified....

    static List<string> staticVar = new List<string>();

    static void Main(string[] args)
    {
        Action<IEnumerable<int>> assyncMethod = enumerator =>
                {
                    lock (staticVar)
                        foreach (int item in enumerator)
                            staticVar.Add(item.ToString());


                };

        assyncMethod.BeginInvoke(Enumerable.Range(0, 500000), null, null);
        Thread.Sleep(100);

        Console.Write(staticVar.Count());
        foreach (string item in staticVar)
        {

        }
    }
+5  A: 

In order for a lock to be effective it must be used in all cases that a collection is accessed. Be it reading or writing. So you must add a lock before enumerating the collection

For example

lock (staticVar) {
    Console.Write(staticVar.Count());
    foreach (string item in staticVar) {

    }
}
JaredPar
only to collection or all types ?
Alexandre
The point of a lock is that nothing can enter the lock if it is held elsewhere. The lock can have no effect on a chunk of code if it is not around it. A lock must wrap every section of code which uses a synchronised variable.
Martin