java线程的堆空间共享,两个或多个线程如何在已分配的堆上共享内存?

As the title says, how do two or more threads share memory on the heap that they have allocated? I've been thinking about it and I can't figure out how they can do it. Here is my understanding of the process, presumably I am wrong somewhere.

Any thread can add or remove a given number of bytes on the heap by making a system call which returns a pointer to this data, presumably by writing to a register which the thread can then copy to the stack.

So two threads A and B can allocate as much memory as they want. But I don't see how thread A could know where the memory that thread B has allocated is located. Nor do I know how either thread could know where the other thread's stack is located. Multi-threaded programs share the heap and, I believe, can access one another's stack but I can't figure out how.

I tried searching for this question but only found language specific versions that abstract away the details.

Edit:

I am trying not to be language or OS specific but I am using Linux and am looking at it from a low level perspective, assembly I guess.

解决方案

My interpretation of your question: How can thread A get to know a pointer to the memory B is using? How can they exchange data?

Answer: They usually start with a common pointer to a common memory area. That allows them to exchange other data including pointers to other data with each other.

Example:

Main thread allocates some shared memory and stores its location in p

Main thread starts two worker threads, passing the pointer p to them

The workers can now use p and work on the data pointed to by p

And in a real language (C#) it looks like this:

//start function ThreadProc and pass someData to it

new Thread(ThreadProc).Start(someData)

Threads usually do not access each others stack. Everything starts from one pointer passed to the thread procedure.

Creating a thread is an OS function. It works like this:

The application calls the OS using the standard ABI/API

The OS allocates stack memory and internal data structures

The OS "forges" the first stack frame: It sets the instruction pointer to ThreadProc and "pushes" someData onto the stack. I say "forge" because this first stack frame does not arise naturally but is created by the OS artificially.

The OS schedules the thread. ThreadProc does not know it has been setup on a fresh stack. All it knows is that someData is at the usual stack position where it would expect it.

And that is how someData arrives in ThreadProc. This is the way the first, initial data item is shared. Steps 1-3 are executed synchronously by the parent thread. 4 happens on the child thread.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值