When you lock an object is that object locked throughout the whole application?
For Example, this snippet from C# 3.0 in a Nutshell Section 19.6.1 "Thread Safety and .NET Framework Types":
static void AddItems( )
{
for (int i = 0; i < 100; i++)
lock (list)
list.Add ("Item " + list.Count);
string[] items;
lock (list) items = list.ToArray( );
foreach (string s in items) Console.WriteLine (s);
}
Does the first lock:
lock (list)
list.Add ("Item " + list.Count);
prevent another thread from accessing:
lock (list) items = list.ToArray( );
or can both be executed at the same time?
And does the CLR automatically make your static methods thread safe? Or is that up to the developer?
Thanks, John