当前位置:网站首页>C# 多线程锁整理记录
C# 多线程锁整理记录
2022-07-24 14:00:00 【墨池象牙白】
ManualResetEvent
new ManualResetEvent(true) //一开始不阻塞
WaitOne();//等待信号
Reset(); //线程会阻塞在WaitOne调用的位置 (一般我喜欢将这个封装一下,命名为Lock,Set则为Unlock
Set();//线程不会阻塞,WaitOne直接通过
AutoResetEvent
new AutoResetEvent(false) //一开始不阻塞,这里和Manual是相反的
调用一次Set,解除阻塞,当WaitOne通过一个线程后,自动再次阻塞,相当于内部自动Reset了
Mutex
类似AutoResetEvent
ReleaseMutex(Set)的时候释放
有一个线程通过WaitOne就会立刻阻塞(Reset)
lock
常见的有lock一个static object或者object
静态与否主要取决于关心的内容是静态变量还是当前实例的变量
Monitor
lock(obj)本身就相当于
Monitor.Enter(obj);
Monitor.Exit(obj);
Interlocked
线程安全的简单的变量操作
public int State
{
get => Interlocked.Increment(ref _state);
}
SpinLock
C#线程锁(自旋锁SpinLock、互斥锁Mutex、混合锁Monitor | lock)_xujianjun229的博客-CSDN博客_c# 线程锁
自旋锁是指当一个线程在获取锁对象的时候,如果锁已经被其它线程获取,那么这个线程将会循环等待,不断的去获取锁,直到获取到了锁。适合于原子操作时间非常短的场景
优点:避免了线程上下文切换。性能较高。
缺点:如果长时间等待,将消耗大量的CPU资源。而且多个等待中的线程,并不是等待时间越长就先获取到锁,可能会一直等待下去。
private static SpinLock _spinLock = new SpinLock();
private static int incrValue = 0;//共享资源
private void Run()
{
bool locked = false;
_spinLock.Enter(ref locked);//获取锁
incrValue++; //安全的逻辑计算
if (locked) //释放锁
_spinLock.Exit();
}
ReaderWriterLockSlim 读写锁
.NET 同步与异步之锁(ReaderWriterLockSlim)(八)_mb5fe09d2d96fc4的技术博客_51CTO博客
ReaderWriterLockSlim.EnterUpgradeableReadLock Method (System.Threading) |微软文档 (microsoft.com)
EnterUpgradeableReadLock主要是用于,需要读,还要写的情况
SemaphoreSlim
构造的时候设置一个数量,限制进入的线程数
SemaphoreSlim 类 (System.Threading) | Microsoft Docs
CancellationTokenSource
这个倒不是锁。刚好看到 顺便记录一下。
C# 使用CancellationTokenSource取消多线程_tnblog技术分享~的博客-CSDN博客_cancellationtokensource
CancellationTokenSource cts = new CancellationTokenSource();
在线程中执行主要逻辑之前调用:
cts.Token.ThrowIfCancellationRequested();
在某处调用了 cts.Cancel(true);
当线程执行到ThrowIfCancellationRequested的位置,会抛出异常,直接退出线程
如果线程已经执行到ThrowIfCancellationRequested之后了,cts就起不了作用了。
边栏推荐
- [untitled]
- Default color setting in uiswitch off state
- Nessus安全测试工具使用教程
- Network security - filtering bypass injection
- Stack and queue - 20. Valid parentheses
- 一些简单命令
- Network security - war backdoor deployment
- Add an element to the object array with unshift
- [C language note sharing] - dynamic memory management malloc, free, calloc, realloc, flexible array
- R语言使用epiDisplay包的dotplot函数通过点图的形式可视化不同区间数据点的频率、使用by参数指定分组参数可视化不同分组的点图分布、使用cex.Y.axis参数指定Y轴分组标签文本的大小
猜你喜欢

Uni app background audio will not be played after the screen is turned off or returned to the desktop

OWASP ZAP安全测试工具使用教程(高级)

Network security - file upload whitelist bypass

Nmap安全测试工具使用教程

RHCE first operation

rhce第一次作业

position: -webkit-sticky; /* for Safari */ position: sticky;

Source code analysis of ArrayList

2022.7.22 模拟赛

2021-07-09
随机推荐
在LNMP架构中搭建Zabbix监控服务
Nmap security testing tool tutorial
Flink fault tolerance mechanism (V)
Ggarrange function of R language ggpubr package combines multiple images and annotates them_ Figure add annotation, annotation, annotation information for the combined image, and add annotation inform
The KAP function of epidisplay package in R language calculates the value of kappa statistics (total consistency, expected consistency), analyzes the consistency of the results of multiple scoring obj
如何在树莓派上搭建运行 WordPress
The fourth edition of probability and mathematical statistics of Zhejiang University proves that the absolute value of the correlation coefficient of random variables X and Y is less than 1, and some
Network security - file upload whitelist bypass
The R language uses the DOTPLOT function of epidisplay package to visualize the frequency of data points in different intervals in the form of point graphs, uses the by parameter to specify the groupi
【无标题】
Wildcard (Pan domain name) SSL certificate
Network security -- Service Vulnerability scanning and utilization
R language uses the sum function of epidisplay package to calculate the descriptive statistical summary information of the specified variables in dataframe under different grouping variables, visualiz
CAS atomic type
一些简单命令
Network security - file upload content check bypass
Solve the problem of repeated clicking of button uibutton
Flinktable & SQL (VI)
Network security - penetration using evil maid physical access security vulnerabilities
Hcip day 13