Reputation: 1953
I see the use of terms Thread Context
and Synchronization Context
being used in documents regarding threading. Do they refer to the same thing?
This is the definition for thread context by Microsoft:
The thread context includes all the information the thread needs to seamlessly resume execution, including the thread's set of CPU registers and stack, in the address space of the thread's host process.
As far as I understand, threads in a thread pool share the same synchronization context. Does that mean they have the same thread context?
Upvotes: 2
Views: 2236
Reputation: 1232
The synchronization context and thread context are 2 very different things. The synchronization context is a method that can queue a unit of work to a context (mostly a different thread). Here is a quote.
One aspect of SynchronizationContext is that it provides a way to queue a unit of work to a context. Note that this unit of work is queued to a context rather than a specific thread. This distinction is important, because many implementations of SynchronizationContext aren’t based on a single, specific thread.
A typical example where synchronization context is useful are the GUI applications like WinForms or WPF apps. In WinForms and WPF, only a single UI thread can update the UI elements (textboxes, checkboxes, etc.). If you try to change the content of a textbox from another non-UI thread, the change doesn't happen or exception could be thrown (depending on the UI framework). So in such application the worker non-UI threads need to schedule all changes to the UI elements to the UI thread. And that is what the synchronization context provides. It allows you to post a unit of work (execution of some method) to a different context - the UI thread in this case.
On the other hand the thread context is a structure that contains all information that is necessary to execute thread code by the OS. If the OS needs to change the execution from one thread to another, it executes something that is called a context switch. In context switch the thread that is currently running on the CPU is frozen by the OS and the current state of all CPU registers is stored to a thread context structure of the frozen thread. Here is the actual thread context structure on Windows OS and here on x64 Windows. When the content of all CPU registers is moved to a thread context of the frozen thread, the OS finds another (the most prior) thread that has to run its code, and moves the content of its thread context structure to the CPU registers. After this, the context switch is over and the CPU can execute the code of the most prior thread until another context switch happens.
So the synchronization context and the thread context are two very different concepts. The thread context is the low level structure that allows the OS to switch between the threads while the synchronization context is a mechanism to simplify sending of work items to the different contexts (mostly the different threads).
Upvotes: 3