无锁
多个关联数据无法完整获取修改
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
ThreadPool.QueueUserWorkItem(Thread1);
ThreadPool.QueueUserWorkItem(Thread2);
ThreadPool.QueueUserWorkItem(Thread3);
Console.ReadKey();
}
public static int[] Datas = new int[3];
public static void Thread1(object o)
{
Datas[0] = 100;
Thread.Sleep(5000);
Datas[1] = 101;
Thread.Sleep(2000);
Datas[2] = 120;
}
public static void Thread2(object o)
{
while (true)
{
Console.WriteLine($"Thread2:0->{Datas[0]},1->{Datas[1]},2->{Datas[2]}");
Thread.Sleep(1000);
}
}
private static void Thread3(object? state)
{
while (true)
{
Console.WriteLine($"Thread3:0->{Datas[0]},1->{Datas[1]},2->{Datas[2]}");
Thread.Sleep(1000);
}
}
}
锁
加上lock锁
每次需要等其他线程解锁了才能用
自旋锁
遇到上锁时,短暂循环等待,线程不挂起
internal class Program
{
static int _lock = 0;
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
Console.WriteLine($"time:{DateTime.Now.ToString("HH:mm:ss.fff")}");
ThreadPool.QueueUserWorkItem(Thread1);
ThreadPool.QueueUserWorkItem(Thread2);
ThreadPool.QueueUserWorkItem(Thread3);
Console.ReadKey();
}
public static int[] Datas = new int[3];
public static void Thread1(object o)
{
while (Interlocked.Exchange(ref _lock, 1) != 0)
{
Thread.SpinWait(1);
}
Datas[0] = 100;
Thread.Sleep(5000);
Datas[1] = 101;
Thread.Sleep(2000);
Datas[2] = 120;
Console.WriteLine($"write finish,time:{DateTime.Now.ToString("HH:mm:ss.fff")}");
//lock (Datas)
//{
//}
Interlocked.Exchange(ref _lock, 0);
}
public static void Thread2(object o)
{
while (true)
{
while (Interlocked.Exchange(ref _lock, 1) != 0)
{
Thread.SpinWait(1);
}
//lock (Datas)
//{
Console.WriteLine(
$"Thread2:0->{Datas[0]},1->{Datas[1]},2->{Datas[2]},time:{DateTime.Now.ToString("HH:mm:ss.fff")}"
);
Thread.Sleep(1000);
//}
Interlocked.Exchange(ref _lock, 0);
}
}
private static void Thread3(object? state)
{
while (true)
{
while (Interlocked.Exchange(ref _lock, 1) != 0)
{
Thread.SpinWait(1);
}
Console.WriteLine(
$"Thread3:0->{Datas[0]},1->{Datas[1]},2->{Datas[2]},time:{DateTime.Now.ToString("HH:mm:ss.fff")}"
);
Thread.Sleep(1000);
Interlocked.Exchange(ref _lock, 0);
}
}
}
读写锁
写入时,唯一线程写入,读的时候可以多个线程同时读取
internal class Program
{
static ReaderWriterLockSlim _lock = new ReaderWriterLockSlim();
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
Console.WriteLine($"time:{DateTime.Now.ToString("HH:mm:ss.fff")}");
ThreadPool.QueueUserWorkItem(Thread1);
ThreadPool.QueueUserWorkItem(Thread2);
ThreadPool.QueueUserWorkItem(Thread3);
Console.ReadKey();
}
public static int[] Datas = new int[3];
public static void Thread1(object o)
{
_lock.EnterWriteLock();
try
{
Datas[0] = 100;
Thread.Sleep(5000);
Datas[1] = 101;
Thread.Sleep(2000);
Datas[2] = 120;
Console.WriteLine($"write finish,time:{DateTime.Now.ToString("HH:mm:ss.fff")}");
}
catch (Exception) { }
finally
{
_lock.ExitWriteLock();
}
}
public static void Thread2(object o)
{
while (true)
{
_lock.EnterReadLock();
try
{
Console.WriteLine(
$"Thread2:0->{Datas[0]},1->{Datas[1]},2->{Datas[2]},time:{DateTime.Now.ToString("HH:mm:ss.fff")}"
);
Thread.Sleep(1000);
}
catch (Exception) { }
finally
{
_lock.ExitReadLock();
}
}
}
private static void Thread3(object? state)
{
while (true)
{
_lock.EnterReadLock();
try
{
Console.WriteLine(
$"Thread3:0->{Datas[0]},1->{Datas[1]},2->{Datas[2]},time:{DateTime.Now.ToString("HH:mm:ss.fff")}"
);
Thread.Sleep(1000);
}
catch (Exception) { }
finally
{
_lock.ExitReadLock();
}
}
}
}