I am Reading Joe's Albahari C# threading tutorial:
Author explains why DateTime.Now
needs to be thread-safe:
Wrapping access to an object around a custom lock works only if all concurrent threads are aware of — and use — the lock. This may not be the case if the object is widely scoped. The worst case is with static members in a public type. For instance, imagine if the static property on the DateTime struct, DateTime.Now, was not thread-safe, and that two concurrent calls could result in garbled output or an exception. The only way to remedy this with external locking might be to lock the type itself — lock(typeof(DateTime)) — before calling DateTime.Now. This would work only if all programmers agreed to do this (which is unlikely). Furthermore, locking a type creates problems of its own.
For this reason, static members on the DateTime struct have been carefully programmed to be thread-safe.
According to MS docs, .NOW
is public static DateTime Now { get; }
, i.e. read-only property.
Why bother with thread-safety, if it is read-only ? Two concurrent calls should be able to get the current time without interfering with each other ?
Edit: A lot of people, pointing out that questions is not very clear. I did make an assumption that it should be safe, because: it is read only and because it is time(always changing).