As we know threads belong to same process run simultaneously using the same shared address space for themselves, does it mean that memory space also gets shared simultaneously among threads, if yes then how ? why do we need context switching if they all are capable of using memory space simultaneously?
2 Answers
why do we need context switching if they all are capable of using memory space simultaneously?
Thread context switch is about CPU time not memory mapping. Imagine that you have two equally important worker threads in order to both threads do their job they have to receive CPU time fairly from the scheduler. That is why you need thread context switch. Fact that they work inside this same process makes this context switch lighter in compare to process context switch nevertheless thread context switch is necessary. During thread context switch virtual memory space remains this same however registers like IP (instruction pointer), SP (stack pointer), general purpose registers are reloaded because other "executioner" gets CPU time.
The other part of your questions is already answered in this Stack Overflow thread.
The main distinction between a thread switch and a process switch is that during a thread switch, the virtual memory space remains the same, while it does not during a process switch. Both types involve handing control over to the operating system kernel to perform the context switch. The process of switching in and out of the OS kernel along with the cost of switching out the registers is the largest fixed cost of performing a context switch.
A more fuzzy cost is that a context switch messes with the processors cacheing mechanisms. Basically, when you context switch, all of the memory addresses that the processor "remembers" in it's cache effectively become useless. The one big distinction here is that when you change virtual memory spaces, the processor's Translation Lookaside Buffer (TLB) or equivalent gets flushed making memory accesses much more expensive for a while. This does not happen during a thread switch.

- 1
- 1

- 1,613
- 14
- 20
-
Thanks for your answer but i would like to clarify if context switching is all about CPU time then threads must be using it one after another as per their priority which suggests that even memory space can't be used simultaneously because we may have many threads in running state simultaneously but still one will execute before others due to the CPU time allotted to them.So, why do we say threads are running simultaneously. – Manish Aug 16 '15 at 10:30
-
We say threads are running simultaneously because they can run simultaneously, but not all threads run at once. A CPU core can run one thread at once. If you have a two core CPU then two threads can run simultaneously. So if we have a program with three threads there will have to be a context switch to run the third thread. – shf301 Aug 29 '15 at 13:54
Each thread has its own stack and a place in the executable.
A context switch is the processor fetching the correct instruction for that thread with the correct stack and values for the registers

- 59,252
- 17
- 87
- 127