为什么线程共享堆空间? [英] Why do threads share the heap space?

查看:686
本文介绍了为什么线程共享堆空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每个线程都有自己的堆栈,但是它们共享一个公共堆。

Threads each have their own stack, but they share a common heap.

对于每个人来说,堆栈是局部/方法变量。堆是实例/类变量。

Its clear to everyone that stack is for local/method variables & heap is for instance/class variables.

在线程之间共享堆的好处是什么。

What is the benefit of sharing heap among threads.

几个线程同时运行的数量,所以共享内存可能导致并发修改,互斥等开销的问题。
堆中的线程共享什么内容。

There are several number of threads running simultaneously, so sharing memory can lead to issues such as concurrent modification, mutual exclusion etc overhead. What contents are shared by threads in heap.

为什么会这样?为什么不是每个线程都有自己的堆呢?任何人都可以提供一个现实世界的例子,线程如何使用共享内存?

Why is this the case? Why not have each thread own its own heap as well? Can anyone provide a real world example of this, how shared memory is utilized by threads?

推荐答案

将数据从一个线程传递到另一个线程? (如果你从来没有这样做,你将编写单独的程序,而不是一个多线程程序。)有两种主要方法:

What do you do when you want to pass data from one thread to another? (If you never did that you'd be writing separate programs, not one multi-threaded program.) There are two major approaches:


  • 您似乎认为理所当然的方法是共享内存:除非有强制性理由是特定于线程(例如堆栈)的数据,所有数据都可以被所有线程访问。基本上,有一个共享堆。这给你速度:任何时候线程更改一些数据,其他线程可以看到它。 (限制:如果线程在不同的处理器上执行,那么这是不正确的:程序员需要特别努力以正确地使用共享内存。)大多数主要命令语言,特别是Java和C# ,赞成这个模型。

  • The approach you seem to take for granted is shared memory: except for data that has a compelling reason to be thread-specific (such as the stack), all data is accessible to all threads. Basically, there is a shared heap. That gives you speed: any time a thread changes some data, other threads can see it. (Limitation: this is not true if the threads are executing on different processors: there the programmer needs to work especially hard to use shared memory correctly and efficiently.) Most major imperative languages, in particular Java and C#, favor this model.

每个线程可以有一个堆,加上一个共享堆。这需要程序员决定将哪些数据放在哪里,并且通常不能与现有的编程语言完美地结合。

It is possible to have one heap per thread, plus a shared heap. This requires the programmer to decide which data to put where, and that often doesn't mesh well with existing programming languages.

消息传递:每个线程都有自己的数据空间;当线程想要与另一个线程通信时,它需要显式地向另一个线程发送消息,以便将数据从发送者的堆拷贝到接收者的堆。在这个设置中,许多社区喜欢调用线程进程。这给你安全:因为一个线程不能覆盖一些其他线程的内存whim,避免了很多错误。另一个好处是分发:您可以使您的线程在单独的机器上运行,而不必更改程序中的单行。你可以找到大多数语言的消息传递库,但集成往往不太好。理解邮件传递的好语言是 Erlang JoCaml

The dual approach is message passing: each thread has its own data space; when a thread wants to communicate with another thread it needs to explicitly send a message to the other thread, so as to copy the data from the sender's heap to the recipient's heap. In this setting many communities prefer to call the threads processes. That gives you safety: since a thread can't overwrite some other thread's memory on a whim, a lot of bugs are avoided. Another benefit is distribution: you can make your threads run on separate machines without having to change a single line in your program. You can find message passing libraries for most languages but integration tends to be less good. Good languages to understand message passing in are Erlang and JoCaml.

事实上,消息传递环境通常在后台使用共享内存,至少线程在同一台机器/处理器上运行。这节省了大量的时间和内存,因为将消息从一个线程传递到另一个线程,然后不需要复制数据。但由于共享内存不会暴露给程序员,因此其固有的复杂性局限于语言/库实现。

In fact message passing environments usually use shared memory behind the scene, at least as long as the threads are running on the same machine/processor. This saves a lot of time and memory since passing a message from one thread to another then doesn't require making a copy of the data. But since the shared memory is not exposed to the programmer, its inherent complexity is confined to the language/library implementation.

这篇关于为什么线程共享堆空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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