threadingTheCode_answers
threadingTheCode_answers
11
Self-Review Questions
Self-review 11.1 What is a ’thread’ and what is a ’process’? What is the difference
between the two?
Self-review 11.2 What does the ’scheduler’ in an operating system take responsi-
bility for?
if __name__ == ’__main__’ :
pool = [ Foobar ( ) for i in range ( 10 ) ]
map ( lambda t : t.start ( ) , pool )
Self-review 11.6 On one occasion when we ran the code above, we got the follow-
ing output:
Thread-1
Thread-2
Thread-7
Thread-3
90 Threading the Code
Thread-9
Thread-8
Thread-6
Thread-10
Thread-4
Thread-5
Self-review 11.7 To the nearest second, how long will the code in Self-review 11.5
take to execute?
Self-review 11.8 The second threaded guessing game program can take up to 5 s
to terminate after the user has entered Ctlr+d. Why is this?
When the user presses Ctrl+d, the main thread sets the keepGoing variable in
the generator thread to False and then terminates. The generator thread could
just have tested the value and entered a sleep of up to 5 s. Only then will it
see the changed value, terminate the loop, terminate the run method and hence
terminate.
Self-review 11.9 ’Livelock’ and ’deadlock’ are two problems that are commonly
associated with multi-threaded programs. Define ’deadlock’ and
’livelock’.
Self-review 11.13 Why must the run method be overridden in subclasses of threading.Thread?
Self-review 11.15 Imagine you are writing a multi-player game. Your game will
have some interesting graphics that need to be generated quickly
to provide a smooth animation. There will be a sound track to
the game-play and several players will be interacting at once.
Inside the game there will be both player and non-player char-
acters. What parts of this program would you want to separate
out into their own threads?
Programming Exercises 91
Programming Exercises
Exercise 11.1 Write a program that creates two threads, each of which should sleep
for a random number of seconds, then print out a message which
includes its thread ID.
Exercise 11.2 Starting with your code from the previous exercise, add a counter to
your program. Each thread should include the value of the counter
in the message it prints to the screen and should increment the counter
every time it wakes up. So, the counter will hold the total number of
messages printed to the screen by all the running threads.
You will need to use some form of concurrency control to ensure
that the value of the counter is always correct. Several of these are
available in Python. One simple solution is to use locks. The idea
here is that if a variable is locked, it cannot be changed by code in
another thread until it has been unlocked. In this way we can use
locks to create critical sections in our code.
Here’s a code fragment to get you started:
import threading
Exercise 11.3 Python provides a special data structure in a class called Queue.Queue
that is specifically for use in multi-threaded programs. You have
already learned about queue types in Sections ?? (page ??) and ??
(page ??). In Python’s queues, data can be added to a queue using a
method called put and removed from it using the method get. Here’s
an example:
Challenges
Challenge 11.2 The classic problem in the area of concurrent programming, was
proposed by Edsgar Dijkstra in 1971, and recast by Tony Hoare as
the The Dining Philosophers Problem.
Five philosophers are sitting at a round table. Philosophers are
quite eccentric, all they do all day is think and eat, but they can’t
Challenges 93