0
sınıf özellik defenition 2 yolu (A & B) karşılaştırılması
:(C#) Sınıf üyesi başlatılıyor parçacığı güvenlik sorunu
// implemention A
public class Cache {
private object m_syncRoot = null;
public object SyncRoot {
get {
if (m_syncRoot == null) {
Interlocked.CompareExchange(ref m_syncRoot, new object(), null);
}
return m_syncRoot;
}
}
}
VE
// implemention B.
public class Cache {
public object SyncRoot { get; } = new object(); // in C# 6.0
}
Ve nihayet biraz önbellek kullanır:
static Cache MyCache = new Cache(); // I don't know if this kind of declaration is thread-safe either
lock (MyCache.SyncRoot) {
....
}
Soru:
"Önbellek" statik örnek olarak kullanılacaktır, her ikisi de [A] & [B] iş parçacığı güvenli "SyncRoot" kreasyonları nelerdir?