首页 > 代码库 > Interlocked.CompareExchange

Interlocked.CompareExchange

class SourceManager
{
private SourceManager() { }

private static SourceManager sourceManager;
public static SourceManager Instance
{
get
{
if (sourceManager == null)
{
/*

lock 实现方式
功能与以下 Interlocked.CompareExchange 相同

lock (this)
{
if (sourceManager == null)
{
sourceManager = new SourceManager();
}
}

*/
Interlocked.CompareExchange<SourceManager>(ref sourceManager, new SourceManager(), null);
}
return sourceManager;
}
}
}

Interlocked.CompareExchange