Locking in C#
Since none of the code you've written modifies the static field after initialization, there is no need for any locking. Just replacing the string with a new value won't need synchronization either, unless the new value depends on the results of a read of the old value.
Static fields aren't the only things that need synchronization, any shared reference which could be modified is vulnerable to synchronization issues.
class Foo
{
private int count = 0;
public void TrySomething()
{
count++;
}
}
You might suppose that two threads executing the TrySomething method would be fine. But its not.
- Thread A reads the value of count (0) into a register so it can be incremented.
- Context switch! The thread scheduler decides thread A has had enough execution time. Next in line is Thread B.
- Thread B reads the value of count (0) into a register.
- Thread B increments the register.
- Thread B saves the result (1) to count.
- Context switch back to A.
- Thread A reloads the register with the value of count (0) saved on its stack.
- Thread A increments the register.
- Thread A saves the result (1) to count.
So, even though we called count++ twice, the value of count has just gone from 0 to 1. Lets make the code thread-safe:
class Foo
{
private int count = 0;
private readonly object sync = new object();
public void TrySomething()
{
lock(sync)
count++;
}
}
Now when Thread A gets interrupted Thread B cannot mess with count because it will hit the lock statement and then block until Thread A has released sync.
By the way, there is an alternative way to make incrementing Int32s and Int64s thread-safe:
class Foo
{
private int count = 0;
public void TrySomething()
{
System.Threading.Interlocked.Increment(ref count);
}
}
Regarding the second part of your question, I think I would just go with whichever is easier to read, any performance difference there will be negligible. Early optimisation is the root of all evil, etc.
Why threading is hard
Reading or writing a 32-bit or smaller field is an atomic operation in C#. There's no need for a lock in the code you presented, as far as I can see.
It looks to me that the lock is unnecessary in your first case. Using the static initializer to initialize bar is guaranteed to be thread safe. Since you only ever read the value, there's no need to lock it. If the value's never going to change, there will never be any contention, why lock at all?