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

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

问题描述

我知道当操作系统/硬件在不同线程的执行之间切换时,它会管理存储/恢复每个线程的上下文,但是我不知道很多细节.我的问题是:是否有任何寄存器可用于在线程之间共享信息?在 x86 中?米普?手臂?等等,.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?

非常感谢任何有关如何做到这一点的建议.

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. 注册

如果您与 Ada 人交谈,他们会因缺乏 linuxposix 线程的定义而惊慌失措.他们更喜欢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.

第二个nd问题是什么是注册?对于大多数人来说,它们仅限于 8,16 或 32 个硬编码在 CPU 指令集中的寄存器.经常有可以通过其他方式访问的二等寄存器.主要是它们的速度非常快.

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操作系统上下文切换代码非常熟悉.可能不清楚的是,每次线程运行时,堆栈 寄存器的高位 之类的东西可能是恒定的;但每个线程都不同.也就是说每个线程都有自己的栈.

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天全站免登陆