.NET Framework 中线程安全类的示例?
我目前正在编写应该是线程安全的代码。许多异步调用和事件以及通常需要大量工作才能保持同步和线程安全的东西。
.NET 框架中是否有任何类可以处理此类事情,我可以查看(反编译)这些类,以了解应该如何完成事情?确实越复杂越好...
I'm currently working on code which is supposed to be thread safe. Lots of asynchronous calls and events and stuff that generally requires quite a bit of work to keep synchronized and thread safe.
Are there any classes in the .NET framework which deal with this sort of thing, which I could look at (decompile), to see how things are supposed to be done? The more complex the better really...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
.NET 4.0 线程安全集合
虽然面向 Windows 而不仅仅是 .NET 、Joe Duffy 的书值得注意:Windows 上的并发编程
.NET 4.0 Thread-Safe Collections
Though targeting Windows rather than just .NET, Joe Duffy's book is worth noting: Concurrent Programming on Windows
MSDN 有一些关于 .NET 异步编程的好信息。查看异步编程设计模式。
另请查看 监视器 和 Mutex 类
MSDN has some good information on asynchronous programming in .NET. Check out Asynchronous Programming Design Patterns.
Also check out the Monitor and Mutex classes in System.Threading
有几种方法可以使事物保持原子性。有些类支持防护,这样可以保证代码按特定顺序执行。您可以使用关键字 volatile 来确保变量访问是原子的(每次使用时从内存中读取)并且没有幻读。这些只是一些工具;我建议使用简单的 Google 搜索“atomic C#、F#..”
There are a few ways to keep things atomic. There are classes that support fencing, such that code is guaranteed to execute in a certain order. You can use the keyword volatile to ensure that variable access is atomic (Read from memory each use) and that you have no phantom reads. These are just a few tools; I would suggest a simple Google of "atomic C#, F#.."