|CS 321 Spring 2012 > Lecture Notes for Friday, February 3, 2012|
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.
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”.
for an example of the use of coroutines
in the Python programming language.
(Kernel) threads will be our topic for the next few days.
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.
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.
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.