Backgroung worker C#.Net 4 [英] Backgroung worker C# .Net 4

查看:48
本文介绍了Backgroung worker C#.Net 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题。

如果我使用.Net中提供的背景工作来做一些密集的工作,它会在同一个核心(我有一个四核机器)上运行父线程(创建此后台工作者的主线程)?

如果我制作大约5-10个后台工作人员,他们会从不同的核心开始..?

他们都会工作真正并行吗?

它们会帮助应用程序利用处理器的多核功能吗?

a后台工作是在新线程上启动的(对吗?),所以它与使用线程池和使用parallel.For和parallel.Invoke有什么不同吗?

我需要清理这些概念。

I have a question.
If I use a backgroungworker provided in .Net to do some intensive work, will it run on on the same core (I have a quad-core machine) as the parent thread (main thread that created this background worker)?
If I make about 5-10 background workers will they start on different cores .. ?
will they all work as truly parallel ?
will they help the application utilize the multi core feature of the processor at all?
a backgroundworker is started on a new thread (right ?) , so is it any differnet from using thread pool and using parallel.For and parallel.Invoke ?
I need to clear up these concepts.

推荐答案

您的问题,按顺序:

也许。

可能。

可能不是。

是。

是。

是。



如果你有N个核心,那么N个线程可以真正并行运行(前提是它们不想访问s ame资源,包括内存对象)。然而,并非系统中的所有线程都是你的 - 当没有更高优先级的线程在等待以及系统决定运行它们时,你的所有线程都会开始(它会花时间切片给它们一个,但是执行顺序取决于它)

因此,后台工作程序可以在与主线程相同的内核上执行,但不能同时执行。如果系统中有N + 1个线程要运行,并且有N个核心,则一个线程必须等待其他线程中的任何一个停止,或者等待资源,或者系统判定它已经它已经并且启动额外的线程。添加比处理器更多的线程会降低整体执行速度,因为启动和交换线程会产生开销。



后台工作程序使用与线程池和并行相同的机制.For,它只是更简单易用,但结果不像Thread Pool那样精确可控,或者特别限制为Parallel.For
Your questions, in order:
Maybe.
Possibly.
Probably not.
Yes.
Yes.
Yes.

If you have N cores, then N threads can truly run in parallel (provided they do not want to access the same resources, including memory objects). However, not all the threads in the system are yours - yours will "get a go" when there are no higher-priority threads waiting and when the system decides to run them (it does time-slice to give them a go, but the execution order is up to it)
As a result, a Background worker may execute on the same core as the main thread, but not at the same time. If you have N+1 threads in the system to run, and N cores, one thread will have to wait until either one of the other threads stops, or waits foe a resource, or the system decides it has had "it's go" and starts the extra thread. Adding more threads than processors can slow the overall execution, as there is an overhead in starting and swapping threads.

A Background worker uses the same mechanism as Thread Pool and Parallel.For, it's just simpler to use, but as a result not as precisely controllable as the Thread Pool, or as specifically limited as Parallel.For


首先,你需要创建多个只有当您需要并行独立的处理逻辑时才需要线程,这些逻辑由应用程序的语义决定。如果您希望使用线程提高性能,那么只有当线程数超过内核数时,才能使其更糟糕。请记住,多线程会产生一些相当大的开销。



现在,关于真正的并行。线程使用它们使用的核心。但是,您可以使用线程和进程 affinity 来影响核心的使用。请参阅我过去的答案:

如何编程多核 [ ^ ] 。



-SA
First of all, you need to create multiple threads only if your need parallel independent processing logic dictated by the semantic of your application. If you hope to improve performance using threads, you well make it only worse if the number of threads exceed the number of cores. Remember that multithreading incurs some considerable overhead.

Now, about "truly parallel". The threads use the cores they use. However, you can affect the use of the cores using thread and process affinity. Please see my past answer:
How to program multicore[^].

—SA


参见按计时器间隔问题每1000毫秒进行一次数据库连接 [ ^ ]



当我最初开发此应用程序时,它使用Web服务一次一个地访问九个不同的服务器来收集数据。大概花了十分钟。当我更改我的软件以便创建九个线程,每个线程从九个服务器之一检索数据时,应用程序在不到1.5分钟的时间内运行。我仍然不知道用户的PC有多少核心。我从来没有看过。这似乎并不重要。他们对结果很满意!
See Solution 1 of Database connection every 1000 milliseconds by Timer interval problem[^]

When I originally developed this application, it accessed nine different servers one at a time using Web Services to gather data. That took about ten minutes. When I changed my software so that it created nine threads that each retrieve the data from one of the nine servers, the application ran in less than 1.5 minutes. I still don't know how many cores the user's PC has. I never looked. It didn't seem to matter. They were pleased with the result!


这篇关于Backgroung worker C#.Net 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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