.net 线程与并行处理
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2331659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Threading vs. Parallel Processing
提问by Icemanind
Microsoft .NET 4.0 introduces new "parallel enhancements" to its framework. I am wondering what the difference between making an application that uses the standard System.Threading functions versus the new parallel enhancements.
Microsoft .NET 4.0 为其框架引入了新的“并行增强”。我想知道制作使用标准 System.Threading 函数的应用程序与新的并行增强功能之间有什么区别。
回答by Aaronaught
Probably the most important difference between the Parallel Extensions and regular threading is the control flow.
并行扩展和常规线程之间最重要的区别可能是控制流。
A thread, created using new Thread(...)or ThreadPool.QueueUserWorkItemwill terminate at a completely indeterminate point in time. If you write this code:
使用new Thread(...)或ThreadPool.QueueUserWorkItem将在完全不确定的时间点终止的线程。如果你写这个代码:
ThreadPool.QueueUserWorkItem(() =>
{
Thread.Sleep(1000);
Console.WriteLine("Work Finished");
});
Console.WriteLine("Item Queued");
The text Item Queuedwill appear right away, and Work Finishedwill be printed after about a 1 second delay.
文本Item Queued将立即出现,并Work Finished在大约 1 秒延迟后打印。
On the other hand, if you write something similar using parallel extensions:
另一方面,如果您使用并行扩展编写类似的内容:
Parallel.For(0, 10, i =>
{
Thread.Sleep(1000);
Console.WriteLine("Test {0}", i);
});
Console.WriteLine("Finished");
What you'll see in this case is a delay of 1 second before anything happens, then a slew of "Test" messages in a random order, and thenthe text Finished.
在这种情况下,您将看到任何事情发生之前的 1 秒延迟,然后是一系列随机顺序的“测试”消息,然后是文本Finished.
In other words, running tasks in parallel does not actually alter the program flow. It will run different tasks on different threads so that they can be executed on multiple CPU cores, in order to improve overall throughput of the program, but as far as the typical programmer is concerned, these tasks aren't really running in the "background" as they would be with a thread. You don't have to change your program's structure or do anything special to be notified when the work completes. You have no control over what happens insidethe parallel block, but you do know that the block will not return control until all parallel tasks are complete.
换句话说,并行运行任务实际上并没有改变程序流程。它会在不同的线程上运行不同的任务,以便它们可以在多个 CPU 核上执行,以提高程序的整体吞吐量,但对于典型的程序员而言,这些任务并不是真正运行在“后台” “就像他们用一根线一样。您无需更改程序结构或执行任何特殊操作即可在工作完成时收到通知。您无法控制并行块内发生的事情,但您知道在所有并行任务完成之前,该块不会返回控制权。
Although Parallel Extensions are great for this, it bears mentioning that PX are of no use whatsoever when you actually needto run a task in the background, such as implementing scheduler, or delegating to a worker thread in order to keep a UI responsive. You still need to use threads or async components for those.
尽管 Parallel Extensions 对此非常有用,但值得一提的是,当您确实需要在后台运行任务时,PX 毫无用处,例如实现调度程序,或委派给工作线程以保持 UI 响应。您仍然需要为这些使用线程或异步组件。
回答by Adam Driscoll
Here's a good channel9 I watched a while back on this topic: http://channel9.msdn.com/posts/philpenn/Parallel-Programming-with-NET-Parallel-Extensions/
这是一个很好的频道 9 我在这个话题上看过一段时间:http: //channel9.msdn.com/posts/philpenn/Parallel-Programming-with-NET-Parallel-Extensions/
回答by Brian Rasmussen
The parallel framework uses the .NET threading model underneath which in turn builds on the Windows threading model. However, a lot of optimization has been done in the general framework to make the parallel library more efficient.
并行框架在其下使用 .NET 线程模型,而该模型又建立在 Windows 线程模型之上。但是,在通用框架中做了很多优化,使并行库更加高效。
This bloghas additional details.
这个博客有更多的细节。
回答by J-16 SDiZ
Parallel Processing is just some fancy interface for auto creating threads. It is easier to use the parallel processing extension for most task.
并行处理只是一些用于自动创建线程的奇特界面。对于大多数任务,使用并行处理扩展更容易。

