4 ms·
This explanation is a bit hand-wavy, but "correct enough" for our purposes. Let us suppose that you wanted to store some per-thread data. A naive way to do th
by dblohm7 6y ago
This explanation is a bit hand-wavy, but "correct enough" for our purposes.
Let us suppose that you wanted to store some per-thread data.
A naive way to do that could be to have a global mapping from thread ID to a pointer to your data. To look up your data, you would obtain the id of your current thread and look it up in the map. Of course, since that map is shared across threads, you'd also need to protect it with a mutex.
Operating systems themselves need to maintain per-thread data. When context-switching between threads, the OS will also switch out the current block of per-thread data. Applications can piggyback on this, so instead of needing to lock a shared map and then do a lookup to retrieve their data, it will already be available to them by virtue of the fact that it is essentially part of the current thread's context. No locking or global map lookups necessary!