Information Technology Reference
In-Depth Information
Cooperative multithreading
Although most thread systems include a scheduler that can—at least in principle—run
any thread at any time, some systems provide the abstraction of cooperative threads.
In these systems, a thread runs without interruption until it explicitly relinquishes control
of the processor to another thread. An advantage of cooperative multithreading is in-
creased control over the interleavings among threads. For example, in most cooperative
multithreading systems, only one thread runs at a time, so while a thread is running, no
other thread can run and affect the system's state.
Unfortunately, cooperative multithreading has significant disadvantages. For exam-
ple, a long-running thread can monopolize the processor, starving other threads and
making the system's user interface sluggish or nonresponsive. Additionally, modern
multiprocessor machines run multiple threads at a time, so one would still have to rea-
son about the interleavings of threads even if cooperative multithreading were used.
Thus, although cooperative multithreading was used in some signficant systems in the
past, including early versions of Apple's MacOS operating system, cooperative multi-
threading is less often used today.
The alternative we describe in the main body is sometimes called preemptive multi-
threading since a running thread can be preempted without explicitly relinquishing the
processor. In the rest of this topic, when we talk about multithreading, we are talking
about preemptive multithreading unless we explicitly state otherwise.
Programmer's
View
Possible
Execution
#1
.
.
.
x = x + 1;
y = y + x;
z = x + 5y;
.
.
.
Possible
Execution
#2
.
.
.
x = x + 1
..............
thread is suspended
other thread(s) run
thread is resumed
...............
y = y + x
z = x + 5y
Possible
Execution
#3
.
.
.
x = x + 1
y = y + x
...............
thread is suspended
other thread(s) run
thread is resumed
................
z = x + 5y
.
.
.
x = x + 1;
y = y + x;
z = x +5y;
.
.
.
Figure4.3: Threads virtualize the underlying hardware's xed number of
processors to allow programmers to use any number of threads.
Search WWH ::




Custom Search