是否有一些 Visual Studio 扩展,用于检查代码是否存在潜在的线程不安全性?
Is there some Visual Studio extension, that checks code for potential thread unsafeness?
是否有一些 Visual Studio 扩展,可以检查代码中潜在的线程不安全性并标记这段代码?也许 ReSharper 具有此功能?
我知道有两个 R# 检查。
对于lock
-语句
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);
}
}
双重检查锁定实现
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;
}
}
是否有一些 Visual Studio 扩展,可以检查代码中潜在的线程不安全性并标记这段代码?也许 ReSharper 具有此功能?
我知道有两个 R# 检查。
对于lock
-语句
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);
}
}
双重检查锁定实现
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;
}
}