当前位置:网站首页>C multithreaded lock collation record
C multithreaded lock collation record
2022-07-24 14:10:00 【Ink pool Ivory】
ManualResetEvent
new ManualResetEvent(true) // Don't block at first
WaitOne();// Wait for the signal
Reset(); // Threads will block in WaitOne Call location ( Generally, I like to package this , Name it Lock,Set Then for Unlock
Set();// Threads don't block ,WaitOne Directly through
AutoResetEvent
new AutoResetEvent(false) // Don't block at first , Here and Manual It's the opposite
Call once Set, unblocked , When WaitOne After passing through a thread , Automatic re blocking , Equivalent to internal automatic Reset 了
Mutex
similar AutoResetEvent
ReleaseMutex(Set) Release when you're in
A thread passes WaitOne It will immediately block (Reset)
lock
Common are lock One static object perhaps object
Whether static or not mainly depends on whether you care about static variables or variables of the current instance
Monitor
lock(obj) Itself is equivalent to
Monitor.Enter(obj);
Monitor.Exit(obj);
Interlocked
Thread safe simple variable operation
public int State
{
get => Interlocked.Increment(ref _state);
}
SpinLock
Spin lock refers to when a thread is acquiring a lock object , If the lock has been acquired by another thread , Then this thread will wait circularly , Keep getting the lock , Until we get the lock . It is suitable for scenarios with very short atomic operation time
advantage : Avoid thread context switching . Higher performance .
shortcoming : If you wait a long time , Will consume a lot of CPU resources . And multiple waiting threads , It's not that the longer you wait, the first you get the lock , May wait forever .
private static SpinLock _spinLock = new SpinLock();
private static int incrValue = 0;// Shared resources
private void Run()
{
bool locked = false;
_spinLock.Enter(ref locked);// Get the lock
incrValue++; // Secure logical computation
if (locked) // Release the lock
_spinLock.Exit();
}
ReaderWriterLockSlim Read-write lock
EnterUpgradeableReadLock Mainly for , Need to read , Also write about
SemaphoreSlim
Set a quantity when constructing , Limit the number of incoming threads
SemaphoreSlim class (System.Threading) | Microsoft Docs
CancellationTokenSource
This is not a lock . I just saw By the way .
CancellationTokenSource cts = new CancellationTokenSource();
Call before executing the main logic in the thread :
cts.Token.ThrowIfCancellationRequested();
Somewhere called cts.Cancel(true);
When the thread executes ThrowIfCancellationRequested The location of , It throws an exception , Exit the thread directly
If the thread has executed to ThrowIfCancellationRequested After that ,cts It doesn't work .
边栏推荐
- 小熊派 课程导读
- 在EXCEL表格中如何进行快速换行
- 交换
- 茅台冰淇淋“逆势”走红,跨界之意却并不在“卖雪糕”
- SQL Server 启停作业脚本
- Apache2 ha experiment with raspberry pie
- Detailed explanation of MSTP protocol for layer 3 switch configuration [Huawei ENSP experiment]
- 【机器学习】之 主成分分析PCA
- binlog、iptables防止nmap扫描、xtrabackup全量+增量备份以及redlog和binlog两者的关系
- Sringboot-plugin-framework 实现可插拔插件服务
猜你喜欢

对话框管理器第二章:创建框架窗口

Unity pedestrians walk randomly without collision

Network security - Cookie injection

Regular expression and bypass cases

Source code analysis of ArrayList

After five years of contact with nearly 100 bosses, as a headhunter, I found that the secret of promotion was only four words

达梦实时主备集群搭建

Soft link, hard link

【NLP】下一站,Embodied AI

Mmdrawercontroller first loading sidebar height problem
随机推荐
IntelliSense of Visual Studio: 'no members available'
如何在树莓派上搭建运行 WordPress
小熊派 课程导读
C language -- program environment and preprocessing
R语言使用sort函数排序向量数据实战、返回实际排序后的数据(默认升序)
解决 uni-starter 使用本地函数可以登录微信 但是使用云函数登录失败
Lazy loading of pictures
sql server语法—创建数据库
The solution to the error of [installation detects that the primary IP address of the system is the address assigned by DHCP] when installing Oracle10g under win7
Soft link, hard link
交换
C language -- three ways to realize student information management
Network security - file upload competitive conditions bypass
binlog、iptables防止nmap扫描、xtrabackup全量+增量备份以及redlog和binlog两者的关系
Data modification modification
Nmap security testing tool tutorial
Detailed analysis of common command modules of ansible service
Dameng real-time active and standby cluster construction
R语言使用epiDisplay包的dotplot函数通过点图的形式可视化不同区间数据点的频率、使用by参数指定分组参数可视化不同分组的点图分布、使用cex.Y.axis参数指定Y轴分组标签文本的大小
字符串——剑指 Offer 58 - II. 左旋转字符串