并行编程C# [英] parallel programming c#

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

问题描述

是与线程相关的并行编程.
我想了解多线程,想知道并行编程是否可以教我这一点?
谢谢

Is parallel programming to do with threading.
I would like to learn about multi threading and wondering if parallel programming teaches me that?
Thanks

推荐答案

Google是您的朋友.这应该可以帮助您入门.

在MSDN上 [ ^ ]
Google is your friend. This should get you started.

On MSDN [^]


这是相关的,但并不完全相同,特别是当您考虑任务并行库或并行扩展.

多线程意味着创建许多线程,如果有多个处理器,则可以并行执行.问题在于创建新线程是一项资源消耗任务,因此不利于快速操作.

例如,如果您能够创建将并行运行的两个线程(每个线程都在其自己的cpu中),那么它就是真正的并行执行.通常我们不知道线程在哪里执行,它们可以从一个处理器跳转到另一个处理器.考虑到创建线程需要花费大量时间和资源,我们应该避免在非常小的任务中使用线程.

这就是为什么有ThreadPool的原因.那里准备好执行许多小任务的线程(如果可能的话,可以并行执行).我们总是可以创建自己的线程池,因为.Net ThreadPool有其自身的局限性.

最后,还有并行扩展/任务并行库.它在内部使用线程和ThreadPool,但经过优化可并行执行许多常见操作.
例如,如果您有四个CPU,则可以对超过一百万个项目执行a操作,并且它将尝试仅使用4个线程(每个cpu一个)来完成这项工作.当然,它要比这复杂得多,但目的是要易于使用.
虽然使用线程通常需要线程同步,但是TPL已经完成了所需的所有同步,因此您的工作很简单,就是将其传递给集合以执行类似ForEach的操作,并且它将尝试对其进行并行化.
哪个更好?
那要看. TPL适用于真正独立的事物或许多小型工作.如果您需要专用的线程来处理线程,线程仍然会更好.
It is related, but it is not exactly the same thing, specially if you think about the Task Parallel Library or Parallel Extensions.

Multi-threading means creating many threads, which can execute in parallel if multi-processors are available. The problem is that creating a new thread is a resource consuming task, so it is not good for fast operations.

If you are capable of creating, for example, only two threads that will run in parallel (each in its own cpu) then it is a real Parallel execution. Usually we don''t know where the threads are executing and they can jump from one processor to another. Considering how time and resource intensive it is to create threads we should avoid doing it for very small tasks.

That''s why there is the ThreadPool. There are threads there prepared to execute many small tasks (in parallel, if possible). We can always create our own threadpool, as the .Net ThreadPool has its own limitations.

Finally, there is the Parallel Extensions/Task Parallel Library. Internally it uses threads and the ThreadPool, but it is optimized to do many common things in parallel.
For example, if you have four CPUs, you can do a for over one million items, and it will try to use only 4 threads (one per cpu) to do the job. Surely it is much more complex than that, but the purpose is to be simple to use.
While using threads usually require Thread Synchronization, the TPL already has all the synchronization needed done, so your job is simple to pass it a collection to do something like a ForEach, and it will try to parallelize it.

Which one is better?
That depends. TPL is better for real independent things or a lot of small jobs. Threads can still be better if you want a dedicated thread for something.


我们必须对看起来像同步操作的对象使用多线程.这是编程的规范,但是事情在不断变化和发展,Microsoft最终使我们能够在我们正在为其编程的系统上利用多核处理器.多线程编程是并行的,但是并行编程本质上不是多线程的.除非多线程发生在单个内核上,否则在这种情况下只能是并发的.在这里,并行实际上是同时发生的,而并发看起来就像是同时发生的.

如果您想详细了解并行编程,建议您阅读本文[
We had to use multi-threading for something that looked like synchronously operations. This was the norm for programming, but things are changing and evolving and Microsoft has finally given us the ability to take advantage of multi-core processors on the systems we''re programming for. Multithreaded programming is parallel, but parallel programming is not essentially multithreaded. Unless the multithreading happens on a single core, in which case it is only concurrent. Here parallel is truly simultaneous, while concurrent just looks as if it was simultaneous.

If you want to know in detail of Parallel Programming I would suggest you to read the Article[^]


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

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