惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Threatpost
V
Vulnerabilities – Threatpost
TaoSecurity Blog
TaoSecurity Blog
C
Cybersecurity and Infrastructure Security Agency CISA
P
Proofpoint News Feed
G
GRAHAM CLULEY
S
Securelist
P
Palo Alto Networks Blog
MongoDB | Blog
MongoDB | Blog
A
Arctic Wolf
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
WordPress大学
WordPress大学
Project Zero
Project Zero
T
Threat Research - Cisco Blogs
L
Lohrmann on Cybersecurity
C
Cyber Attacks, Cyber Crime and Cyber Security
F
Fortinet All Blogs
博客园 - 叶小钗
B
Blog RSS Feed
C
Cisco Blogs
Google DeepMind News
Google DeepMind News
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Apple Machine Learning Research
Apple Machine Learning Research
G
Google Developers Blog
K
Kaspersky official blog
D
Docker
Latest news
Latest news
Cisco Talos Blog
Cisco Talos Blog
T
Tor Project blog
Cyberwarzone
Cyberwarzone
Security Latest
Security Latest
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Spread Privacy
Spread Privacy
Microsoft Azure Blog
Microsoft Azure Blog
C
Check Point Blog
J
Java Code Geeks
Simon Willison's Weblog
Simon Willison's Weblog
T
Tenable Blog
Recent Announcements
Recent Announcements
T
Tailwind CSS Blog
H
Help Net Security
L
LINUX DO - 热门话题
T
The Exploit Database - CXSecurity.com
Jina AI
Jina AI
S
SegmentFault 最新的问题
MyScale Blog
MyScale Blog
NISL@THU
NISL@THU
美团技术团队
腾讯CDC

博客园 - Yunanw

应用SQLServer For XML 生成XML避免在C# 拼字符串 某些版本的IIS可能有SessionID混淆的Bug 小觑数据库(SqlServer)查询语句执行过程 通过表达式树实现的ObjectMapper 实际一个多态(或弱类型)ConfigurationElementCollection 通过P/Invoke调用32位C++ DLL时,一定要将平台改为X86 坑爹的面试题 在Windows2008 IIS7上部署.net 1.1 用Python 实现刷钻网上抢任务,并实现一个简单的限制使用时间的功能 Python几行代码打印出网卡的Mac地址 放大招,解决前同事签出TFS文件的问题 飞猪传书 Linq To xml 查询时需要注意的一点。 - Yunanw 自定义WinForm 下ListBox的行高 使用LogParser分析网站运行情况(比较简单) 重新装载VS的模板 咱也搞一个TransactionScope QQ斗地主记牌器(只支持角色版) - Yunanw - 博客园 实现支持会话的WebClient
C#中Finally的一个不太常见的用法
Yunanw · 2014-04-14 · via 博客园 - Yunanw

最近在看.net BCL 传送门 的源码. 在

System.Collections.Concurrent.ConcurrentQueue

中看到一段有意思的代码.注意这段代码是写在ConcurrentQueue这个用于并发中的队列. 注意,这是一个无锁队列的实现.

   try
    { }
    finally
    {
        newhigh = Interlocked.Increment(ref m_high);
        if (newhigh <= SEGMENT_SIZE - 1)
        {
            m_array[newhigh] = value;
            m_state[newhigh].m_value = true;
        }
        if (newhigh == SEGMENT_SIZE - 1)
        {
            Grow();
        }
    }

有意思吗?代码中使用了一个空的Try代码块.然后把代码全都写在了Finally块.这么做的目地何在呢?

这其实是一个小的技巧:放在Finally中的代码可以防止执行线程在执行过程中被另一个线程用调用了Thread.Abort()或Thread. Interrupt()打断.从而保证这段代码执行的完整性.

举个例子: 如果不将上面代码放到Finally中运行.假如正好有一个线程A执行到 m_array[newhigh] = value;而另外一个线程B调用了线程A的Thread.Abort() 那么m_array[newhigh] = value; 以后的代码可能没有机会得到执行.那么将引起ConcurrentQueue的不完整.

而放到Finally中的代码,即使线程B在线程A执行时调用了Thread.Abort()或Thread. Interrupt()方法时也能保证Finally块中的代码被完整的执行.

事实上,这个特性是在.net framework2.0中引入的.在.net 1.1时Finally没有这个作用.另外 Tread.Abort有可能打断线程内的静态构构函数执行.

另外我认为同样的功能也可以用这个Thread.BeginCriticalRegion 和Thread.EndCriticalRegion(); 来实现.