I wonder if there is a guarantee in the .NET about a value read by some thread from a field write by another thread.
This question is not about syncing the threads via sync primitives. I do not care about race conditions or a thread-safety. Also I do not asking a field volatility since for me it is not important to have not writes in original order.
Let check simple example
private bool m_isBusy;
public bool IsBusy
{
get => m_isBusy;
set
{
m_isBusy = value;
NotifyPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
InvokeOnUIThread
(
() => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName))
);
}
So there is a view model (constructed by the UI thread or by another thread for instance by an IoC container). Looking into NotifyPropertyChanged
it is obvious that PropertyChangedEventHandler
will run on the UI thread so it is sure it will be the UI thread that will check for a new value.
So what guarantees in this particular scenario where any thread can set IsBusy
that the UI thread will find there the value that was set and not the value it has there already❔
There is good question regard this on StackOverflow (Can a C# thread really cache a value and ignore changes to that value on other threads?) but it seems little outdated.
I am almost 100 % sure that I am aware that any thread will get an actual value (not some CPU cached value, or other cached one (?)) but I cannot find the precept which tells this is guaranteed.
Is there some official statements about such guarantees?