如何编写安全的/正确的多线程code。在.NET? [英] How to write safe/correct multi-threaded code in .NET?

查看:112
本文介绍了如何编写安全的/正确的多线程code。在.NET?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天,我不得不修正其使用线程一些老VB.NET 1.0 code。问题是从工作线程,而不是UI线程更新UI元素。我花了一些时间来发现,我可以使用断言与InvokeRequired发现问题。

Today I had to fix some older VB.NET 1.0 code which is using threads. The problem was with updating UI elements from the worker thread instead of the UI-thread. It took me some time to find out that I can use assertions with InvokeRequired to find the problem.

除了上面提到的并发修改的问题,也有死锁,比赛条件等人能碰上。 作为调试/修复线程问题是一种痛苦,我不知道我怎么会降低编码在这方面的错误/故障,我如何能更容易找到他们。所以,我问什么是:

Besides the above mentioned concurrent modification problem, there are deadlocks, race conditions, etc. one could run into. As debugging/fixing threading problems is a pain, I'm wondering how I could reduce coding errors/faults in this area and how I could easier find any of them. So, what I'm asking for, is:

  • 有什么好看的图案编写多线程code时应遵循的?什么该做什么和不该做什么?
  • 您用来调试线程问题什么技术呢?
  • Are there any good patterns to follow when writing multi-threading code? What are the Dos and Don'ts?
  • What techniques do you use to debug threading problems?

请(如果适用),并可能提供一些例如code。这些问题的答案应与在.NET框架(任何版本)。

Please provide some example code if applicable and possible. The answers should be related to the .NET framework (any version).

推荐答案

这可能是一个的大规模列表 - 读乔·达菲的优秀的并行编程在Windows 对于更多的细节。这是pretty的太大的倾吐心事......

This could be a massive list - read Joe Duffy's excellent "Concurrent Programming On Windows" for much more detail. This is pretty much a brain dump...

  • 尝试,以免调用到了code显著块,而你自己的锁
  • 避免锁定在参考文献,其中价值类外C $ C也可能会锁定在
  • 如果你需要同时获得多个锁,始终掌握在相同的顺序这些锁
  • 如果合理,使用一成不变的类型 - 他们可以自由地在线程之间共享
  • 除了稳定的类型,尽量避免需要线程之间共享数据
  • 不要试图让你的类型的线程;大多数类型不需要是,通常它需要共享数据的code将需要控制锁定本身
  • 在WinForms应用程序:
    • 请不要在UI线程上执行任何长时间运行或阻塞操作
    • 请不要从非UI线程以外的任何线程触摸用户界面。 (使用BackgroundWorker的,Control.Invoke /的BeginInvoke)
    • Try to avoid calling into significant chunks of code while you own a lock
    • Avoid locking on references which code outside the class might also lock on
    • If you ever need to acquire more than one lock at a time, always acquire those locks in the same order
    • Where reasonable, use immutable types - they can be shared freely between threads
    • Other than immutable types, try to avoid the need to share data between threads
    • Avoid trying to make your types threadsafe; most types don't need to be, and usually the code which needs to share data will need to control the locking itself
    • In a WinForms app:
      • Don't perform any long-running or blocking operations on the UI thread
      • Don't touch the UI from any thread other than the UI thread. (Use BackgroundWorker, Control.Invoke/BeginInvoke)

      在调试方面,我并没有非常多的建议。使用了Thread.Sleep提振看到竞争条件和死锁可以工作的机会,但你得有一个什么样的错误,你知道在哪里把它前一个相当合理的理解。日志记录是非常方便的,但不要忘记,code进入一种量子态 - 通过记录观察它几乎必然要改变自己的行为。

      In terms of debugging, I don't have very much advice. Using Thread.Sleep to boost the chances of seeing race conditions and deadlocks can work, but you've got to have quite a reasonable understanding of what's wrong before you know where to put it. Logging is very handy, but don't forget that the code goes into a sort of quantum state - observing it via logging is almost bound to change its behaviour!

      这篇关于如何编写安全的/正确的多线程code。在.NET?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆