并行编码与多线程(在单CPU上)

发布于 2024-07-25 13:19:37 字数 141 浏览 2 评论 0原文

我们可以在单CPU上互换使用“并行编码”和“多线程编码”吗?

我在这两方面都没有太多经验, 但我想将我的编码风格转变为上述任何一种。

我发现现在很多单线程应用程序都已经过时了,对于未来软件行业的职业前景来说,哪一个更好?

can we use interchangeably "Parallel coding" and "Multithreading coding " on single cpu?

i am not much experience in both,
but i want to shift my coding style to any one of the above.

As i found now a days many single thred application are obsolete, which would be better for future software industy as a career prospect?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

不如归去 2024-08-01 13:19:38

这个问题有点令人困惑,因为您可以在多个线程中执行并行操作,但并非所有多线程应用程序都使用并行计算。
在并行代码中,通常有许多“工作人员”使用一组数据来异步返回结果。 但多线程的应用范围更广,比如 GUI、阻塞 I/O 和网络。

在单个或多个 CPU 上不会有太大变化,因为管理取决于操作系统如何处理线程和进程。

多线程将在任何地方都有用,并行不是日常计算范例,因此它可能是职业前景中的“利基”。

The question is a bit confusing as you can perform parallel operations in multiple threads, but all multi-thread applications are not using parallel computing.
In parallel code, you typically have many "workers" that consume a set of data to return results asynchronously. But multithread is used in a broader scope, like GUI, blocking I/O and networking.

Being on a single or many CPU does not change much, as the management depends on how your OS can handle threads and processes.

Multithreading will be useful everywhere, parallel is not everyday computing paradigm, so it might be a "niche" in a career prospect.

属性 2024-08-01 13:19:38

我在.NET 4.0 中看到的一些演示,并行代码更改似乎比执行线程更容易。 “For Loops”和其他支持并行处理的东西有新的语法。 所以是有区别的。

我认为将来你会同时做这两件事,但我认为并行支持会更好、更容易。 您仍然需要线程来进行后台操作和其他操作。

Some demos I saw in .NET 4.0, the Parallel code changes seem easier then doing threads. There is new syntax for "For Loops" and other things to support parallel processing. So there is a difference.

I think in the future you will do both, but I think the Parallel support will be better and easier. You still need threads for background operations and other things.

故人的歌 2024-08-01 13:19:38

事实上,您无法在单个 CPU 上实现“真正的”并行性。 有几个库(例如 C 的 MPI)可以在这方面提供一些帮助。 但并行性的概念并没有在致力于流行解决方案的开发人员中使用。

由于在单个 CPU 上引入了多个内核,多线程现在很常见,由于线程库和线程安全类型、方法、类等,在每种语言中实现它都很容易且几乎透明。 这样你就可以模拟并行性。

无论如何,如果您要开始这样做,请先阅读有关并发和线程主题的内容。 当然,线程+并行可以很好地协同工作。

The fact is that you cannot achieve "real" parallelism on a single CPU. There are several libraries (such as C's MPI) that help a little bit on this area. But the concept of paralellism it's not that used among developers working on popular solutions.

Multithreading is common these days thanks to the introduction of multiple cores on a single CPU, it's easy and almost transparent to implement in every language thanks to thread libs and threadsafe types, methods, classes and so on. This way you can simulate paralellism.

Anyway, if you're starting with this, start by reading about concurrency and threading topics. And of course, threads + parallelism work good together.

魂归处 2024-08-01 13:19:38

我不确定您认为“并行编码”是什么,但据我了解,并行编码是指生成由 CPU 并行执行的代码,因此多线程代码属于该描述。

这样,显然您可以互换使用它们(因为一个落入另一个之中)。

尽管如此,我还是建议你慢慢来,从基础开始学习。 了解为什么多线程变得重要,进程、线程和纤程之间有什么区别,如何同步它们等等。

请记住,正如您所说的那样,并行编码非常复杂,特别是与顺序编码相比,因此请做好准备。 也不要急于介入。 仅仅因为您使用 3 个线程而不是 1 个线程,并不会让您的程序变得更快,甚至可能会使其变得更慢。 您需要了解方法和原因。 并非所有事物都可以并行,也不是所有可以并行的事物都应该并行。

I'm not sure about what do you think "Parallel coding" is but Parallel coding as I understand it refers to producing code which is executed in parallel by the CPU, and therefore Multithreaded code falls inside that description.

In that way, obviously you can use them interchangeably (as one falls inside the other).

Nonetheless I'll suggest you take it slowly and start learning from the basics. Understand WHY multithreading is becoming important, what's the difference between processes, threads and fibers, how do you synchronize either of them and so on.

Keep in mind that parallel coding, as you call it, is quite complex, specially compared to sequential coding so be prepared. Also don't just rush into it. Just because you use 3 threads instead of one won't make your program faster, it can even make it slower. You need to understand the hows and the whys. Not every thing can be made parallel and not everthing that can, should.

裸钻 2024-08-01 13:19:38

用简单的语言
CPU 本身可以使用多线程,并且
并行编程是一项显式任务,要么由编译器完成,要么由程序员“#pragma”编写的构造完成

in simple Language
multithreading is available in the CPu by itself and
parallel programming is an explicit task either done by the compiler or my constructs written by programmers "#pragma"

我只土不豪 2024-08-01 13:19:37

多线程和并行编码/计算之间肯定存在重叠,主要区别在于目标处理架构。

多线程已用于在具有共享内存的单个 CPU 上的单个进程中利用并发的优势。 在具有多个 CPU 的计算机上运行相同的程序可能会带来显着的加速,但通常是一种额外的好处,而不是预期的(直到最近)。 许多操作系统都有线程模型(例如pthreads),该模型受益于多个CPU,但不需要多个CPU。

多处理是针对多个CPU的并行编程的标准模型,从早期的SMP机器在一台大机器上有很多CPU,然后到跨很多机器的集群计算,现在又回到了一台计算机上有许多 CPU/核心。 MPI 是一个可以跨多种不同架构工作的标准。

当然,可以使用具有语言框架(如 OpenMP)的线程来编程并行设计。 我听说过多组件 GUI/应用程序依赖于理论上可以在任何地方运行的单独处理。 实际上,前者比后者更多。

主要区别可能是当程序跨多台机器运行时,使用多线程是不切实际的,并且共享内存的现有应用程序将无法工作。

There is definitely overlap between multithreading and parallel coding/computing, with the main differences in the target processing architecture.

Multithreading has been used to exploit the benefits of concurrency within a single process on a single CPU with shared memory. Running the same programs on a machine with multiple CPUs may result in significant speedup, but is often a bonus rather than intended (until recently). Many OSes have threading models (e.g. pthreads), which benefit from but do not require multiple CPUs.

Multiprocessing is the standard model for parallel programming targeting multiple CPUs, from early SMP machines with many CPUs on a big machine, then to cluster computing across many machines, and now back to many CPUs/cores on a single computer. MPI is a standard that can work across many different architectures.

Of course, one can program a parallel design using threads with language frameworks like OpenMP. I've heard of multicomponent GUIs/applications that rely on separate processing that could theoretically run anywhere. Practically, there's more of the former than the latter.

Probably the main distinction is when the program runs across multiple machines, where it's not practical to use multithreading, and existing applications that share memory will not work.

新雨望断虹 2024-08-01 13:19:37

并行编码是并行(同时)执行多个操作的概念。

单处理器上的多线程编程

单处理器上的多线程给人一种并行运行的错觉。 在幕后,处理器根据线程的优先级在线程之间切换。

多个处理器上的多线程编程

多个处理器内核上的多线程是真正的并行。 每个微处理器都运行一个线程。 因此,会同时发生多个并行、并发的任务。

Parallel coding is the concept of executing multiple actions in parallel(Same time).

Multi-threaded Programming on a Single Processor

Multi-threading on a single processor gives the illusion of running in parallel. Behind the scenes, the processor is switching between threads depending on how threads have been prioritized.

Multi-threaded Programming on Multiple Processors

Multi-threading on multiple processor cores is truly parallel. Each microprocessor is running a single thread. Consequently, there are multiple parallel, concurrent tasks happening at once.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文