Information Technology Reference
In-Depth Information
Figure4.7: Output of one possible run of the program in Figure 4.6.
Exercises To complete the following problems, download the sthread li-
brary from sthread.h, and sthread.c. The comment at the top of threadHello.c
explains how to compile and run a program that uses this library.
1. Download this example code threadHello.c, compile it, and run it several
times. What do you get if you run it? Do you get the same thing if
you run it multiple times? What if you are also running some other
demanding processes (e.g., compiling a big program, playing a Flash game
on a website, or watching streaming video) when you run this program?
2. For the threadHello program in Figure ??, what is the maximum number
of threads that could exist while the program is running? (Be careful.)
3. For the threadHello program in Figure ??, suppose that we delete the
second for loop so that the main routine simply creates NTHREADS threads
and then prints \Main thread done." What are the possible outputs of
the program now. Hint: Fewer than NTHREADS+1 lines may be printed in
some runs. Why?
4. How expensive are threads? Write a program that times how long it
taks to create and then join 1000 threads, where each thread simply calls
sthreadexit(0) as soon as it starts running.
5. Write a program that has two threads. The first thread a simple loop that
continuously increments a counter and prints a period (\.")
whenever
 
Search WWH ::




Custom Search