是否有可能线程之间共享一个寄存器? [英] Is it possible to share a register between threads?

查看:1037
本文介绍了是否有可能线程之间共享一个寄存器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,当不同的线程执行之间的OS /硬件开关就管理存储/还原每个线程的上下文中,但是我不知道有多少细节。我的问题是:有,我可以使用线程之间共享信息的登记?在86? MIPS?臂?等等,。 Linux呢?窗口?

I know that when the OS/Hardware switch between the execution of different threads it manage the store/restore the context of each thread, however I do not know many of the details. My question is: are there any register that I can use to share information between threads? In x86? mips? arm? etc,. linux? windows?

如何可以做到这一点的任何建议是一个高度preciated。

Any suggestion on how this can be done is highly apreciated.

推荐答案

您的问题乍看起来合理。其他人曾试图直接回答这个问题。首先,我们有两个相当模糊的概念,

Your question seems reasonable at first glance. Other people have tried to answer the question directly. First we have two fairly nebulous concepts,


  1. 主题

  2. 寄存器

如果你跟阿达乡亲,他们将在缺少的的Linux POSIX 的线程的定义,吓坏了。他们喜欢的东西更喜欢的 Java的的绿色线程具有非常确定性调度。我想你的意思是快速的处理器,如 POSIX 的主题。

If you talk to Ada folks, they will freak out at the lack of definition of a linux or posix threads. They like something more like Java's green threads with very deterministic scheduling. I think you mean threads that are fast for the processor, like posix threads.

2. 第二问题是什么是一个的注册的?对大多数人来说,他们被限制为8,16或32个寄存器,在CPU的指令集硬codeD。经常有,可以通过其他方式来访问二等的寄存器。主要是他们是惊人的快。

The 2nd issue is what is a register? To most people they are limited to 8,16 or 32 registers that are hard coded in the CPU's instruction set. There are often second class registers that can be accessed by other means. Mainly they are are amazingly fast.

你的问题的倒数是相当普遍的。如何将寄存器设置为每个线程的不同的的值。通用寄存器是由编译器的编译器和 ABI 的用途是非常熟悉的的 OS上下文切换的code。什么可能不太清楚的是,物像的高位的的寄存器可能是一个线程运行时不断的每一次;但是为每个线程不同。也就是说,每个线程都有自己的堆栈。

The inverse of your question is quite common. How to set a register to a different value for each thread. The general purpose registers are use by the compiler and the ABI of the compiler is intimately familiar to the OS context switch code. What may not be clear is that things like the upper bits of a stack register may be constant every time a thread runs; but are different for each thread. That is to say that each thread has its own stack.

使用ARM Linux中,一个特殊的协处理器的寄存器用于实施的线程本地存储。在协处理器的寄存器慢比通用访问注册,但它仍然是相当快的。这需要我们的方法和线程之间的差异。

With ARM Linux, a special co-processor register is used to implement thread local storage. The co-processor register is slower to access than a general purpose register, but it is still quite fast. That takes us to the difference between a process and a thread.

一个进程有一个完全不同的内存布局。也就是说,在 MMU 页表转换为不同的进程。用于一个线程,寄存器集可以是不同的,但是所有常规存储器的线程之间共享。出于这个原因,有很多的互斥的,当你做多线程编程。

A process has a completely different memory layout. Ie, the mmu page tables switch for different processes. For a thread, the register set may be different, but all of regular memory is shared between threads. For this reason, there is lots of mutexes when you do thread programming.

现在,考虑CPU高速缓存。它是超高速记忆就像一个通用注册。唯一的区别是指令需要解决这一问题的量。

Now, consider a CPU cache. It is ultra-fast memory just like a general purpose register. The only difference is the amount of instructions it takes to address it.

所有的操作系​​统的的和CPU已经有此!每个线程共享内存以及内存的缓存。从加载两个线程的全局变量缓存的接近一样快,寄存器的访问。由于的螺纹寄存器的你提议只能容纳一个指针,你需要去引用它来访问一些较大的实体。加载的全局变量的将是几乎一样快,编译器就可以把这个在任何注册它喜欢。另外,也可以为编译器在不需要此访问例程使用这些寄存器。因此,即使,如果是保留一个通用操作系统注册成为在相同线程之间,这也只能是一种更快的很小集的应用程序。

All of the OS's and CPUs already have this! Each thread shares memory and that memory is cached. Loading a global variable in two threads from cache is near as fast as register access. As the thread register you propose can only hold a pointer, you would need to de-reference it to access some larger entity. Loading a global variable will be nearly as fast and the compiler is free to put this in any register it likes. It is also possible for the compiler to use these registers in routines that don't need this access. So even, if there was an OS that reserved a general purpose register to be the same between threads, it would only be faster for a very small set of applications.

这篇关于是否有可能线程之间共享一个寄存器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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