CS 321 Spring 2012  >  Lecture Notes for Friday, February 3, 2012

CS 321 Spring 2012
Lecture Notes for Friday, February 3, 2012

Threads [2.2]

Levels of Context Sharing

Note: This is not in the text.

Suppose we want to perform two tasks on a computer. We write code to do these tasks. When these two pieces of code are executed, they could share a great deal of context, or very little. Here are the main options, from most shared to least.

In the same function
If the two pieces of code lie in the same function, then they would share almost everything, even local variables. The only thing they would not share is a time slot; one will be executed first.
We could put the code into separate functions. Then they would be somewhat insulated from each other; in particular, they would have different local variables.
Coroutines, sometimes called user-level threads
Coroutines are different threads of execution that are managed by the application, or the environment that supports the application. Pieces of code in different coroutines share the same data space, but have separate program counters. The idea is generally that two (or more) functions are currently active and communicating via some channel. Which function is actually being executed, goes back and forth, as necessary in order to handle information passing through the channel.

Typically, coroutines are implemented at the language level. Support for coroutines is becoming more and more common in modern programming languages. For example, the programming language Go includes coroutines in the form of the cutesily named “goroutines”. The Python programming language includes limited coroutine support in the form of “generators”.

(Kernel) Threads
Kernel threads, often simply threads, are much like processes, but share the same address space. The separation between such threads is managed at a lower level than coroutines, by the OS kernel.
As above, but different address spaces.
Virtual machines
Here we might run the pieces of code on the same machine, but under different OSs, and possibly different virtual processors.
Remote execution
Last, and most separated, we can run the code on different machines.

See generate.py (NetRun link) for an example of the use of coroutines in the Python programming language.

(Kernel) threads will be our topic for the next few days.

The Basics of Threads

What are Threads?

Threads are like processes, but share a common memory space. For efficiency reasons, threads are often not managed by the kernel with the same mechanism as standard processes.

Why Use Threads?

Threads allow us to do multiple, concurrent operations on a common dataset in a way that is efficient and convenient. Other methods include doing the operations sequentially, in which case one might block, thus delaying all others. Or we could switch between operations in our application code, which is likely to be painful to write.

Threads can be implemented in a “lightweight” manner, with less overhead than kernal processes. This allows threads to be created, terminated, and switched between very efficiently.

Example Use of Threads

In a web server, we might have a master thread that takes requests for web pages, and a number of slave threads that serve the responses.

Threads will be continued next time.

CS 321 Spring 2012: Lecture Notes for Friday, February 3, 2012 / Updated: 7 Feb 2012 / Glenn G. Chappell / ggchappell@alaska.edu