在编程中如何正确使用线程? [英] what constitute proper use of threads in programming?

查看:73
本文介绍了在编程中如何正确使用线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在编程中如何正确使用线程?

听到人们建议您每个处理器仅使用一个线程,而许多程序每个进程最多使用100个线程,我感到厌倦了!以一些常见程序为例

vb.net ide uses about 25 thread when not debugging<br />
System uses about 100<br />
chrome uses about 19<br />
Avira uses more than about 50



每当我发布与线程相关的问题时,几乎每次都会提醒我,每个处理器不应使用一个以上的线程,并且上面提到的所有程序在使用单个处理器的系统上都被破坏了.
怎样在编程中正确使用线程?
请发表一般性评论,但我更喜欢.NET Framework
感谢

解决方案

适当的"线程数量在很大程度上取决于情况和用法.没有一个适合所有答案的大小.可能人们(尤其是初学者)认为添加更多线程可以提高性能,但实际上可能产生相反的效果.

对于Windows应用程序,一次只能更新一个线程,因此无论运行多少线程都没有关系.但是,如果需要进行一些密集的后台处理,例如读取文件并进行一些计算,则比另一个或两个线程有​​用.

您无法对其他应用程序及其使用的线程数量进行比较,因为您不知道它们在做什么.


如果您的应用程序中有任务,则需要要执行它,这将花费可观的时间,那么不应该在UI线程上完成它,因此您需要将其放在后台线程上.任何说您不应该为Microsoft Outlook团队工作的人.

基本上,每当您需要执行不应阻塞应用程序的任务(例如侦听TCP请求)时,请考虑创建一个线程.如果您需要执行仅在设定的时间间隔执行的操作,请考虑将等待该时间间隔的线程放在单独的线程上,然后在该线程上触发进程.

这些都是我在应用程序中所做的所有事情,而且我从未抱怨过这样做.


除了性能之外,还有很多使用线程的原因.
例如,假设您的程序执行了22个不同的操作,那么这些操作中的每个操作都是一个较长的顺序操作,并且在顺序中的各个点上它必须等待输入或通过网络获取数据,或者...

如果这些操作不需要太多共享数据,那么最简单的事情就是在22个不同的线程中运行这22个不同的操作.

这可能不是最快的性能,但是却是最简单的实现.

当然,您可以在更少的线程中实现相同的事情,但是代码却不那么容易阅读或维护.


数据不要使用比内核/处理器更多的线程"完全是错误的.

真实的基准应该是这样的:

为了获得最佳性能,线程与内核的最佳比率取决于任何线程将要在I/O上等待的平均时间,与在任何一个处理器中交换执行线程所花费的时间相比,该时间由共享数进行了修改.考虑到操作系统分配内核的算法,线程将需要专有锁[持续多长时间]的资源.

换句话说:如果您的线程将等待很长的输入时间,并且它们没有任何需要独占锁定(或很少需要访问)的共享资源,那么使用更多线程可能会获得更好的性能.

但是,如果您的线程具有共享的资源,而它们需要经常排他地锁定,那么实际上,如果您只有一个线程,则可能会获得更好的性能,因此不必锁定/等待资源.

what constitute proper use of threads in programming?

I am tired of hearing people recommend that you should use only one thread per processor, while many programs use up to 100 per process! take for example some common programs

vb.net ide uses about 25 thread when not debugging<br />
System uses about 100<br />
chrome uses about 19<br />
Avira uses more than about 50



Any time I post a thread related question, I am reminded almost every time that I should not use more that one thread per processor, and all the programs I mention above are ruining on my system with a single processor.
What constitutes proper use of threads in programming?
Please make general comment, but I''d prefer .NET framework
thanks

解决方案

The "proper" number of threads depends quite a lot on the situation and usage. There is no one size fits all answer. May people, especially beginners, think adding more threads improves performance, but can actually have the opposite effect.

For a windows app, only one thread can update the UI at a time so it doesn''t matter how many you have running. Yet if there is some intense background processing that needs to occur, perhaps reading a file and doing some calculations, than another thread or two may be useful.

You can''t use a comparison of other applications and the number of threads they use because you have no idea what they are doing.


If you have a task in your application that you need to perform and it''s going to take a persceptible amount of time then it shouldn''t be done on the UI thread, so you need to put it onto a background thread. Anybody who says you shouldn''t obviously works for the Microsoft Outlook team.

Basically, any time you need to perform a task (such as listening for TCP requests) that shouldn''t block the application, consider creating a thread. If you need to do something that occurs only at a set interval, then consider putting the wait for that interval on a separate thread - and trigger the process on that thread.

These are all things I''ve done in applications, and I''ve never had complaints about doing it.


There are many reasons for using threads besides perfomance.

For example, suppose your program does 22 different operations, each of those operations is a long sequential operation and at various points in the sequence it has to wait on input or fetch data over a network, or...

If those operations don''t need to share data much, then the easiest thing to do is run those 22 different operations in 22 different threads.

It might not be the fastest performance wise, but it''s the simplest implementation wise.

Sure, you could implement the same thing in fewer threads, but the code wouldn''t be as easy to read or maintain.


And the datum "don''t use more threads than you have cores/processors" is just outright false.

The real datum should be something like:

To get optimum performance the optimum ratio of threads to cores is dependent on the average time any thread is going to be waiting on i/o compared to the time it takes to swap threads of execution in any one processor as modified by the number of shared resources the threads will need to have execlusive locks [for how long], taking into account the OS''s algorithm for allocating cores.

In other words: if your threads are going to have long waits for input and they don''t have any shared resources that the need to exclusively lock (or rarely need access to), then you probably will get better performance with more threads.

But if your threads have shared resources that they need to exclusively lock often, you might actually end up with better performance if you have a single thread so you don''t have to lock / wait for the resource(s).


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

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