什么是重要的线程API调用编写多线程code之前,明白了吗?线程、多线程、重要、明白了

2023-09-06 08:13:48 作者:装纯比装逼更可恨

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

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

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

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

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

解决方案

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

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

多线程2 线程状态 Thread类API

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

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

API Knowledge

Thread ThreadPool BackgroundWorker Asynchronous Operations/Delegates

Shared Resource Multi-threading: Requirements

Basic Multi=-threading requirements Use of shared resources

API Knowledge

Basic Multi-threading API's lock()/Monitor (they are the same thing) Interlocked ReaderWriterLock and variants volatile

Multi-thread Synchronization Requirements

Basic Multi=-threading requirements Shared Resource Multi-threading requirements Synchronization of behavior across multiple threads

API Knowledge

Basic Multi-threading API's Shared Resource Multi-threading API's WaitHandle Manual/AutoResetEvent Mutex Semaphore

Concurrent Shared Resource Multi-threading (hyperthreading) Requirements

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

API Knowledge

Basic Multi-threading API's Shared Resource Multi-threading API's 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.