111,128
社区成员
发帖
与我相关
我的任务
分享public static T Exchange<T>(ref T location1,T value) where T : class
static void SetValue()
{
object status = true;
System.Threading.Interlocked.Exchange(ref status, (object)true);
}
//A simple method that denies reentrancy.
static void UseResource()
{
//0 indicates that the method is not in use.
if(false == Interlocked.Exchange<bool>(bool, true)) //用这个可以完成你要的赋值操作。
{
Console.WriteLine("{0} acquired the lock", Thread.CurrentThread.Name);
//Code to access a resource that is not thread safe would go here.
//Simulate some work
Thread.Sleep(500);
Console.WriteLine("{0} exiting lock", Thread.CurrentThread.Name);
//Release the lock
}
}