什么是重要的线程API调用编写多线程code之前,明白了吗? [英] What are the important threading API calls to understand before writing multi-threaded code?

查看:90
本文介绍了什么是重要的线程API调用编写多线程code之前,明白了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我的博客对经常过度使用的想法多线程在.net中的。我放在一起的盯着列表的API,你应该知道第一:

  • 主题
  • 线程池
  • 的ManualResetEvent
  • 的AutoResetEvent
  • 的EventWaitHandle
  • 的WaitHandle
  • 监控
  • 互斥
  • 旗语
  • 互锁
  • 的BackgroundWorker
  • 的AsyncOperation
  • 锁声明
  • 挥发性
  • ThreadStaticAttribute
  • Thread.MemoryBarrier
  • Thread.VolatileRead
  • Thread.VolatileWrite

然后,我开始思考,也许不是所有的这些都是很重要的。例如,Thread.MemoryBarrier大概可以安全地从列表中删除。再加上明显的声明,我不知道一切,我决定把这里。

因此​​,这是一个广泛的,自以为是的问题,但我很好奇,以集体的意见作为对最佳实践的研究名单。基本上我正在寻找一个简短暗杀名单的新的和/或小开发商从开始到编写多线程code在C#中的时候工作。

因此​​,没有进一步的评论,应该怎样添加或删除从上面的列表?

解决方案

我想你需要多线程,而不是不同的API的级别分类。根据您的线程需要,您可能会或可能不会需要知道你已经列出了API的某些子集。如果我要组织他们,我会做沿着这些路线:

  • 基本多线程:
    • 需求
      1. 需要运行并发进程。
      2. 请不要需要访问共享资源。
      3. 在最大限度地利用现有的硬件资源。
    • API知识
      1. 主题
      2. 线程池
      3. 的BackgroundWorker
      4. 在异步操作/代表
  • 共享资源多线程:
    • 需求
      1. 在基本多= -threading要求
      2. 使用共享资源
    • API知识
      1. 在基本的多线程API的
      2. 在锁定()/监视器(他们是同样的事情)
      3. 互锁
      4. ReaderWriterLock和变型
      5. 挥发性
  • 多线程同步
    • 需求
      1. 在基本多= -threading要求
      2. 在共享资源多线程的要求
      3. 在多个线程的行为同步
    • API知识
      1. 在基本的多线程API的
      2. 在共享资源的多线程API的
      3. 的WaitHandle
      4. 手动/的AutoResetEvent
      5. 互斥
      6. 旗语
  • 并发共享资源多线程(超线程)
    • 需求
      1. 在基本多= -threading要求
      2. 在共享资源多线程的要求
      3. 并行读/写访问共享集合
    • API知识
      1. 在基本的多线程API的
      2. 在共享资源的多线程API的
      3. 并行扩展到.NET / .NET 4.0

的API的我只想物体,一般线程的知识,东西可能被拾起的需要,因为它们适合所有三个层次的休息。比如像MemoryBarrier是pretty的边缘,并且通常有更好的方法来完成它完成,同样的事情歧义到他们的行为和意义更小。

Recently I was blogging about the oft over-used idea of multi-threading in .Net. I put together a staring list for "APIs you should know first":

  • Thread
  • ThreadPool
  • ManualResetEvent
  • AutoResetEvent
  • EventWaitHandle
  • WaitHandle
  • Monitor
  • Mutex
  • Semaphore
  • Interlocked
  • BackgroundWorker
  • AsyncOperation
  • lock Statement
  • volatile
  • ThreadStaticAttribute
  • Thread.MemoryBarrier
  • Thread.VolatileRead
  • Thread.VolatileWrite

Then I started thinking maybe not all of these are important. For instance, Thread.MemoryBarrier could probably be safely removed from the list. Add to that the obvious statement that I don't know everything and I decided to turn here.

So this is a broad and opinionated question, but I'm curious as to the collective's opinion as to a best-practice study list. Essentially I'm looking for a short hit list for new and/or Jr. developers to work from when beginning to write multi-threading code in C#.

So without further commentary, what should be added or removed from the above list?

解决方案

I think you need to classify the levels of multithreading, not the different API's. Depending on your threading needs, you may or may not need to know certain subsets of the API's you have listed. If I were to organize them, I would do it along these lines:

  • Basic Multi-threading:
    • Requirements

      1. Need to run concurrent processes.
      2. Do not need access to shared resources.
      3. Maximizing utilization of available hardware resources.

    • API Knowledge

      1. Thread
      2. ThreadPool
      3. BackgroundWorker
      4. Asynchronous Operations/Delegates

  • Shared Resource Multi-threading:
    • Requirements

      1. Basic Multi=-threading requirements
      2. Use of shared resources

    • API Knowledge

      1. Basic Multi-threading API's
      2. lock()/Monitor (they are the same thing)
      3. Interlocked
      4. ReaderWriterLock and variants
      5. volatile

  • Multi-thread Synchronization
    • Requirements

      1. Basic Multi=-threading requirements
      2. Shared Resource Multi-threading requirements
      3. Synchronization of behavior across multiple threads

    • API Knowledge

      1. Basic Multi-threading API's
      2. Shared Resource Multi-threading API's
      3. WaitHandle
      4. Manual/AutoResetEvent
      5. Mutex
      6. Semaphore

  • Concurrent Shared Resource Multi-threading (hyperthreading)
    • Requirements

      1. Basic Multi=-threading requirements
      2. Shared Resource Multi-threading requirements
      3. Concurrent read/write access to shared collections

    • API Knowledge

      1. Basic Multi-threading API's
      2. Shared Resource Multi-threading API's
      3. Parallel Extensions to .NET/.NET 4.0

The rest of the API's I would simply lump into general threading knowledge, stuff that could be picked up as needed, as they fit into all three levels. Things like MemoryBarrier are pretty fringe, and there are usually better ways to accomplish the same thing it accomplishes, with less ambiguity into their behavior and meaning.

这篇关于什么是重要的线程API调用编写多线程code之前,明白了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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