senpost
4/20/2011 - 6:52 PM

.Net collections are thread-safe for concurrent thread reads

.Net collections are thread-safe for concurrent thread reads

//Concurrent reading is thread safe
static List<int> sourceList = new List<int>() { 1, 2, 3, 4, 5 };

static void Main(string[] args)
{
    new Thread(Iterate).Start();
    new Thread(Iterate).Start();

    Console.ReadKey();
}

static void Iterate()
{
    foreach (var item in sourceList)
    {
        Console.WriteLine("ThreadID: " + Thread.CurrentThread.ManagedThreadId 
            + " ArrayItem: " + item);
        Thread.Sleep(50);
    }
}