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

推荐订阅源

D
Docker
爱范儿
爱范儿
T
The Exploit Database - CXSecurity.com
量子位
T
Tailwind CSS Blog
T
Threatpost
The GitHub Blog
The GitHub Blog
AWS News Blog
AWS News Blog
云风的 BLOG
云风的 BLOG
K
Kaspersky official blog
P
Proofpoint News Feed
博客园 - 司徒正美
L
LangChain Blog
T
Threat Research - Cisco Blogs
C
CERT Recently Published Vulnerability Notes
罗磊的独立博客
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 叶小钗
S
Secure Thoughts
The Last Watchdog
The Last Watchdog
Spread Privacy
Spread Privacy
H
Hacker News: Front Page
T
Troy Hunt's Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News
W
WeLiveSecurity
A
Arctic Wolf
Apple Machine Learning Research
Apple Machine Learning Research
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
P
Proofpoint News Feed
T
Tor Project blog
T
The Blog of Author Tim Ferriss
I
Intezer
P
Privacy & Cybersecurity Law Blog
美团技术团队
N
Netflix TechBlog - Medium
博客园_首页
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Vulnerabilities – Threatpost
Application and Cybersecurity Blog
Application and Cybersecurity Blog
G
Google Developers Blog
Attack and Defense Labs
Attack and Defense Labs
T
Tenable Blog
月光博客
月光博客
Stack Overflow Blog
Stack Overflow Blog
J
Java Code Geeks
腾讯CDC
Microsoft Security Blog
Microsoft Security Blog
A
About on SuperTechFans
Last Week in AI
Last Week in AI

博客园 - kiminozo

UAP如何根据DeviceFamily显示不同的页面 Windows 10 响应式设计和设备友好的开发 Windows Phone Toolkit for WP8 已经出了 代码分享 ScrollViewerListener 获取ScrollViewer的位置改变 WP7技巧 扩展【共享...】按钮 Bangumi 番組計劃 WP手机客户端发布 WP7进阶技巧 自定义Toast 提示动画效果 WP7自定义控件 TabSwitch控件 模拟Windows phone 开始菜单的瓦片动画 WP7自定义控件 评分控件 WP7应用开发笔记 TiltEffect为控件添加倾斜的触控响应效果 从FLC中学习的设计模式系列-结构型模式(3)-享元模式 WP7应用开发笔记 继承BitmapSource并使用独立存储来缓存远程的图片 用代理类包装异步调用方法实现异步命令 解决log4net在.net 4.0 ClientProfile下无法使用 从FLC中学习的设计模式系列-结构型模式(2)-装饰 从FLC中学习的设计模式系列-结构型模式(1)-适配器 从FLC中学习的设计模式系列-创建型模式(5)-原型 从FLC中学习的设计模式系列-创建型模式(4)-建造者
经验 C#手动同步的滥用实例
kiminozo · 2012-04-18 · via 博客园 - kiminozo

刚刚到新公司发现很多同学在用AutoResetEvent作线程同步,有的人用AutoResetEvent代替了C#的lock,

public void Func2()
{
Console.WriteLine("Func2 WaitOne");
autoResetEvent.WaitOne();
 Thread.Sleep(1000); //do Something
Console.WriteLine("Func2 Set");
autoResetEvent.Set();
}

这种写法我还是真是第一次见到。所以不是很确定,结果我研究了一下果然杯具了,重入后直接死锁了。

示例:

class Program
{


static void Main(string[] args)
{
var p = new Program();

p.Func1();/这样会死锁}

AutoResetEvent autoResetEvent = new AutoResetEvent(true);

public void Func1()
{
Console.WriteLine("Func1 WaitOne");
autoResetEvent.WaitOne();
Thread.Sleep(1000);
Func2();
Console.WriteLine("Func1 Set");
autoResetEvent.Set();
}

public void Func2()
{
Console.WriteLine("Func2 WaitOne");
autoResetEvent.WaitOne();
Thread.Sleep(1000);
Console.WriteLine("Func2 Set");
autoResetEvent.Set();
}
}

正确的写法还是用lock或者直接Monitor(try-finally暂时省略),看来这周需要花大功夫整改了。

private readonly object lockObj = new object();

        public void Func3()
        {
            Console.WriteLine("Func3 WaitOne");
            Monitor.Enter(lockObj);
            Thread.Sleep(1000);
            Func4();
            Console.WriteLine("Func3 Set");
            Monitor.Exit(lockObj);
        }

        public void Func4()
        {
            Console.WriteLine("Func4 WaitOne");
            Monitor.Enter(lockObj);
            Thread.Sleep(1000);
            Console.WriteLine("Func4 Set");
            Monitor.Exit(lockObj);
        }