Chapter Objective
- To introduce the notion of a thread
- To discuss the APIs for the Pthreads, Win32, and Java thread libraries
- To examine issues related to multithreaded programming
A thread is a basic unit of CPU utilization; it comprises:
It shares with other threads belonging to the same process its
Multithread programming provides a mechanism for more efficient use of multiple cores and improved concurrency.
Five areas present challenges in programming for multicore systems:
User threads are supported above the kernel and are managed without kernel support, whereas kernel threads are supported and managed directly by the operating system.
Maps user-level threads to one kernel thread.
Thread management is done by the thread library in user space, so it is efficient; but the entire process will block if a thread makes a blocking system call. Also, because only one thread can access the kernel at a time, multiple threads are unable to run in parallel on multiprocessors.
The only drawback to this model is that creating a user thread requires creating the corresponding kernel thread. Because the overhead of creating kernel threads can burden the performance of an application, most implementations of this model restrict the number of threads supported by the system.
developers can create as many user threads as necessary, and the corresponding kernel threads can run in parallel on a multiprocessor. Also, when a thread performs a blocking system call, the kernel can schedule another thread for execution.
A Java thread may be in one of six possible states in the JVM:
Thread cancellation is the task of terminating a thread before it has completed.
Cancellation may occur in 2 different scenarios:
A signal is used in UNIX systems to notify a process that a particular event has occurred.
The mechanism that handle every new request by a new thread has at least two downsides:
One solution: Thread Pool:
Pros:
he number of threads in the pool can be set heuristically based on factorssuch as the number of CPUs in the system, the amount of physical memory, and the expected number of concurrent client requests. More sophisticated thread-pool architectures can dynamically adjust the number of threads in the pool according to usage patterns.
Concerns the communications between kernel and the thread library.
Many systems use a data structure named light weight process, to handle this job.
scheduler activation mechanism:
One case: an application thread is gonna be blocked.
Another case: an event that the blocking thread is waiting for occurs: