English 中文(简体)
需要TPL图书馆的典型问题是什么?
原标题:What are typical problems that would require TPL library?
  • 时间:2012-05-26 06:01:31
  •  标签:
  • c#

I m currently reading Pro. NET 4.0 Parallel Programming in C#, however there are no exercises at the end of each chapter. Though I understand the concept, I still feel lacking some actual practices. I need some real problems to reinforce what I learned. I searched over the internet, but mostly tutorials...
Are there problem sets that target only on TPL? I found this library fascinating in many ways and I just want to ace it. So I wonder if someone could share me some problems in the domain of TPL so that I can practice my knowledge. Any problem or reference that you have encountered would be greatly appreciated. I just need problems, I will find the solution myself. Thanks in advance.

最佳回答

在我看来,TPL完全与可缩放性有关。如果这样想,你会在代码中找到利用它的方法。如果它不是平行的,它会按顺序进行。在一个小的应用程序中,只有很少的动作,顺序处理是巨大的。但如果你对具体过程有成千上万个要求,TPL就会来到这里。

说您想要处理请求列表。 在请求中是网站的 URL 。 程序是下载 HTML 内容, 从页面上拉出特定数据并将其存储到数据库中。 通常这必须按顺序进行。 现在想象有10人同时执行此请求。 按最后的提交按钮的人必须等到所有其他人完成处理后再执行。 这可能需要很长的时间和无限的递增 。

视觉代表:

[Request01] - Finished 
[Request02] - Started
[Request03] - Waiting
[Request04] - Waiting
[Request05] - Waiting
[Request06] - Waiting
[Request07] - Waiting
[Request08] - Waiting
[Request09] - Waiting
[Request10] - Waiting

TPL - 您可以将这些请求存储在同步收藏( TPL 收藏) 中, 并在平行收藏中循环 。 TPL 不仅将这些请求分割成线条并同时运行, 还在处理器的每个核心上运行 。

说您有一个服务器,有两个双核心处理器; 过程会看起来是这样的:

CPU1 Core1
  [Request01] - Finished 
  [Request02] - Started
  [Request03] - Started

CPU1 Core2
  [Request04] - Finished 
  [Request05] - Started
  [Request06] - Started

CPU2 Core1
  [Request07] - Finished 
  [Request08] - Started
  [Request09] - Started

CPU2 Core2
  [Request10] - Finished

如你所见 — — 这可以增加产量,减少等待时间。 TPL的好处是,如果你能开发出一个健全的应用软件,那么可缩放性就会从软件的硬件上掉回到非常大的硬件上。 读者越多,这种服务就越多,你就会投入更多的服务器。 在IT世界中,这更容易被接受。 每个人都愿意扩大他们的硬件,没有人愿意更新他们的软件。

我希望这能帮助你指明正确的方向!

问题回答

I dont think there are special problem sets which can be solved only with TPL.

TPL的目的是使开发者通过简化在应用程序中增加平行和共通程序来提高生产率。TPL以动态的通通货币程度为尺度,以便最高效地使用所有可用的处理器。通过使用TPP,您可以最大限度地提高您的代码的性能,同时侧重于您程序设计要完成的工作。

This is the description / purpose of TPL from msdn, and i think it sums it up nicely;
I think that what you re asking about tpl can generally be asked about multi threading, and im not sure its always easy to tell whether multi threading is the best solution performance wise, or not (except for very obvious cases, when your code needs to access independent data source, or something like that). Event when you decide multi threading is the best solution for your problem, theres no automatic way to tell how many threads to use and how to divide the work between them.
Lets say you have an application that has to send X emails through some web service. Would you send all the X in one big loop? would you divide them into separate N threads and send X/N in each thread? Those questions can sometimes only be answered with trial and performance profling monitoring, and as far as i can tell, the TPL library aims to solve this for you: instead of expecting from you to do all those calculcations (which also might not get you the same result on each execution), the tpl aims to count the required level of parallelism for you dynamicly, and thus, theoretically at least, proivde the best performance in each case.

我与TPL合作过好几次,发现它很有帮助。

  1. I was working on an online payment system. Theoretically, there could be from hundreds to thousands of payments per second. For each payment, I had to call a web service to notify service provider that someone paid n amount of money. Because calling a service is an IO operation, it was out of question to call the service from the site itself, because we would run out of threads from the pool very shortly. That s why I decided to use a job scheduler. The job runs periodically and fetches pending payments (transactions) from database and that s where TPL shines. Sending hundreds or thousands calls to service one by one is very inneficient. That s why I used TPL. It allocates optimal amount of threads automatically and calls appropriate services in parallel. As I said, because web service call is an IO operation, it was ok (and probably a mandatory) to allocate more threads than logical cores on the machine.
  2. Another problem is, when you need to process a large amount of data (which could be represented as an array of collection for example) where IO operation doesn t happen, everything is CPU bound. You can use TPL to distribute load across several cpus/cores. In this situation, it is best to have 1 thread per cpu core.




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签