并发和多线程 [英] Concurrency and Multithreading

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

问题描述

我对并发和多线程等主题不是很有经验。事实上,在我的大多数网络开发职业生涯中,我从来不需要触摸这些主题。

I'm not very experienced with subjects such as Concurrency and Multithreading. In fact, in most of my web-development career I had never needed to touch these subjects.

我觉得这是一个重要的概念,特别是对于桌面应用程序和基本上任何其他不生成HTML的应用程序)。

I feel like it's an important concept, especially for Desktop applications and basically any other application that doesn't generate HTML :).

在阅读了一点并发之后,似乎在Go(google编程语言)和I不太明白为什么一个语言在一个概念上比其他人更好,因为它基本上是能够并行处理和计算东西的。这不是这种编程的工作原理吗?

After reading a bit on concurrency, it seems to be better supported in languages like Go (google programming language) and I don't quite understand why would a language be better than others at a concept like concurrency, since it's basically about being able to fork() processes and compute stuff in parallel, right? Isn't this how programming works?

多线程似乎是一个并发的分支,因为它允许你在同一个进程下并行运行,虽然似乎是

Multithreading seems to be a branch of concurrency as it allows you to run things in parallel beneath the same process, although it seems to be platform specific how it's implemented.

我想我的问题是,为什么特定语言在并发会更好fork()过程是一个更好的解决方案,而不只是使用线程?

I guess my question is, why would specific languages be better at concurrency than others and why would fork()ing processes be a better solution rather than just using threads?

推荐答案

线程与多个进程不同,所以fork()真的不适用这里。

Well for one thing, multiple threads are not the same as multiple processes, so fork() really does not apply here.

多线程/并行处理是很难的。首先,你必须弄清楚如何实际划分要完成的任务。然后你必须协调所有的并行位,这可能需要彼此谈话或共享资源。然后你需要巩固的结果,这在某些情况下可能会有点像前两个步骤一样困难。我在这里简化,但希望你能得到想法。

Multithreading/parallel processing is hard. First you have to figure out how to actually partition the task to be done. Then you have to coordinate all of the parallel bits, which may need to talk to each other or share resources. Then you need to consolidate the results, which in some cases can be every bit as difficult as the previous two steps. I'm simplifying here, but hopefully you get the idea.

所以你的问题是,为什么会有一些语言更好?好的,有几件事情可以让它更容易:

So your question is, why would some languages be better at it? Well, several things can make it easier:


  • 优化的不可变数据结构。你想在可能的并行处理中坚持不变结构,因为它们更容易推理。一些语言对这些有更好的支持,有些具有各种优化,即在没有任何实际复制的情况下将集合拼接在一起,同时仍然实现不变性的能力。

  • Optimized immutable data structures. You want to stick to immutable structures whenever possible in parallel processing, because they are much easier to reason about. Some languages have better support for these, and some have various optimizations, i.e. the ability to splice collections together without any actual copying while still enforcing the immutability. You can always build your own structures like these, but it's easier if the language or framework does it for you.

同步原语和使用它们的简易性。你可以创建自己的结构,像这样,但如果语言或框架为你更容易。当不同的线程做共享状态时,它们需要被同步,并且有许多不同的方式来实现这一点。你得到的同步原语数组越宽,你的任务就越容易。如果您必须与关键部分而不是读取器 - 写入器锁进行同步,性能将会受到影响。

Synchronization primitives and ease of using them. When different threads do share state, they need to be synchronized and there are many different ways to accomplish this. The wider the array of sync primitives you get, the easier your task will ultimately be. Performance will take a hit if you have to sync with a critical section instead of a reader-writer lock.

原子事务。甚至比一大堆同步原语更好的是不必使用它们。数据库引擎非常好;而不是你,程序员,必须弄清楚你需要锁定哪些资源,何时和如何,你只是对编译器或解释器说,这一行下面的所有东西都需要发生在一起,所以确保没有人在我使用它的时候弄乱它。发动机会为你找出锁定。你几乎从来不会在抽象编程语言中获得这种简单性,但越接近你就越好。将多个常见操作合并成一个的线程安全对象是一个开始。

Atomic transactions. Even better than a wide array of sync primitives is not having to use them at all. Database engines are very good at this; instead of you, the programmer, having to figure out exactly which resources you need to lock and when and how, you just say to the compiler or interpreter, "all of the stuff below this line needs to happen together, so make sure nobody else messes around with it while I'm using it." And the engine will figure out the locking for you. You almost never get this kind of simplicity in an abstract programming language, but the closer you can come, the better. Thread-safe objects that combine multiple common operations into one are a start.

自动并行性。让我们说,你必须遍历一个长列表的项目,并以某种方式转换,如乘以50,000 10x10矩阵。如果你只是告诉编译器,不是很好:嘿,每个操作可以独立完成,所以为每个操作使用一个单独的CPU内核?无需自己实际实现线程?有些语言支持这种事情;例如,.NET小组一直在PLINQ上工作。

Automatic parallelism. Let's say you have to iterate through a long list of items and transform them somehow, like multiply 50,000 10x10 matrices. Wouldn't it be nice if you could just tell the compiler: Hey, each operation can be done independently, so use a separate CPU core for each one? Without having to actually implement the threading yourself? Some languages support this kind of thing; for example, the .NET team has been working on PLINQ.

这些只是几个例子,在并行/多线程应用程序中使您的生活更轻松。我相信还有更多。

Those are just a few examples of things that can make your life easier in parallel/multi-threaded applications. I'm sure that there are many more.

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

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