Each a thread ones a program (is a single sequence stream within a process) and has it own register bank. But wait what about systems that has 1 core it have 1 set of register!

It the magic of Thread Scheduler (or just called the scheduker) it switch between threads saving the current register bank and switch the next thread register bank

image.png

Context Switching

image.png

Save execution is the saving of all register values in the stack of the current thread.

Save execution is the saving of all register values in the stack of the current thread.

Restore does the what is says, it restores the register values from the stack of the thread you are restoring.

Restore does the what is says, it restores the register values from the stack of the thread you are restoring.

<aside> šŸ’”

Each thread typically has itsĀ own stack. This is necessary because threads execute independently and may have their own local variables, function calls, and return addresses. Sharing the same stack between threads would lead to corruption and unpredictable behavior as threads overwrite each other's data. In a multithreaded system, the operating system or runtime allocates separate stack spaces for each thread.

TheĀ stackĀ is always located inĀ RAM, not in flash memory. Each thread has its own separate stack region in RAM. The operating system or RTOS allocates a unique stack for each thread, and the stack pointer (SP) register is switched when changing threads.

</aside>

Classification of Threads

image.png

sporadic: like execptions

image.png


Thread Control Block (TCB)

Data Structure containing information private to each thread