2017-07-03 13 views

答えて

2

私が認識しているR#の検査は2種類あります。ダブルチェックロック実装

public class DoubleCheckedLockingWarning 
{ 
    private List<string> _instance; 
    private readonly object _lockObj = new object(); 

    public List<string> GetInstance() 
    { 
     if (_instance != null) return _instance; 

     lock (_lockObj) 
      if (_instance == null) 
       // R# warning: "Possible incorrect implementation of Double-Check Locking. 
       // Checked field must be volatile or assigned from local variable 
       // after 'Thread.MemoryBarrier()' call": 
       _instance = new List<string>(); 

     return _instance; 
    } 
} 
について

lock -statementsについて

public class SometimesNotSynchronizedWarning 
{ 
    private readonly List<string> _list = new List<string>(); 
    private readonly object _lockObj = new object(); 

    public bool Contains(string item) 
    { 
     lock (_lockObj) 
     { 
      return _list.Contains(item); 
     } 
    } 

    public void Add(string item) 
    { 
     // R# warning: "The field is sometimes used inside synchronized block 
     // and sometimes used without synchronization": 
     _list.Add(item); 
    } 
} 

関連する問題