How each thread can have CPU registers

2020-04-09 03:18发布

At several places I have read,

Each thread has its own set of CPU registers and its own stack

Here, I can understand about stack, it's a data structure. But how thread can have its own CPU registers. They are fix in number and there are limitless number of threads are running in the system.

Regards

3条回答
Viruses.
2楼-- · 2020-04-09 03:53

When the scheduler does a context switch (another thread gets its piece of CPU time), it saves current context into memory. The context contains most of the registers (if not all) and it will be "loaded back" from memory into the registers when the thread again gets the CPU.

查看更多
倾城 Initia
3楼-- · 2020-04-09 04:03

Registers are saved/restored when execution switches from/to a thread.

查看更多
家丑人穷心不美
4楼-- · 2020-04-09 04:08

Yes, there are a fixed number of registers.

When the OS switches from 1 thread to another, it copies the state of the finishing thread, including the current values of all the registers into its descriptor, then copies the values from the the descriptor of another thread into the registers.

These descriptors are stored in RAM, although this is in part what Hyper Threading/Hyper Transport set out to fix, allowing 2 threads to be stored in a single CPU core at a time, but that is an entirely different discussion.

查看更多
登录 后发表回答