Information Technology Reference
In-Depth Information
Other per-thread state: Thread-local variables
In addition to the per-thread state that corresponds to execution state in the single-
threaded case, some systems include additional thread-local variables. These variables
are similar to global variables in that their scope spans different procedures, but they
differ in that each thread has its own copy of these variables.
Example: Errno. In UNIX programs a variable called errno stores infor-
mation about any errors that occurred in the most recently executed system
call. In a multi-threaded program, multiple threads can perform system calls
concurrently. Thus, to retrieve the status of the current thread's most recent
library call, errno is typically a macro that maps to a thread-local variable.
Example: Heap internals. Although a program's heap is logically shared—it
is OK for one thread to allocate an object on the heap and then pass a pointer
to that object to another thread—for performance reasons heaps may internally
subdivide their space into per-thread regions. The advantage of subdividing
the heap this way is that multiple threads can each be allocating objects at the
same time without interfering with one another, and cache hit rates may be
better. For this to work, each subdivision of the heap has some thread-local
variables that keep track of what parts are allocated, what parts are free, and
so on. Then, the code that allocates new memory (e.g., malloc() and new() )
are written to use these thread-local data structures.
Thread-local variables are useful, but for simplicity, the rest of our discussion focuses
on just the TCB, registers, and stack as the core pieces of per-thread state.
system needs somewhere to store a thread's registers when that thread is
not actively running on a processor. So, the TCB contains fields in which
the operating system can store a copy of all of the processor's registers.
Per-thread metadata. The TCB also includes include per-thread metadata|
information about each thread. This information is used by the operating sys-
tem to manage the thread. For example, each thread might have a thread ID,
scheduling priority, and any other information the operating system wants to
remember about the thread.
Shared state. In addition to per-thread state that is allocated for each thread,
as Figure 4.8 illustrates, other state is shared by different threads in a multi-
threaded process or multi-threaded operating system.
In particular, the program code is shared by all of the threads in a process.
Additionally, statically-allocated global variables and dynamically-allocated heap
variables can store information that is accessible to all threads.
 
Search WWH ::




Custom Search