Exercises
71. | Implement a set of functions that will suspend and resume threads but also allow you to obtain a thread's suspend count. |
72. | Compare the performance of the parallel search programs, one using threads (Program 7-1, grepMT) and the other using processes (Program 6-1, grepMP). Compare the results with those in Appendix C. |
73. | Perform additional performance studies with grepMT where the files are on different disk drives or are networked files. Also determine the performance gain, if any, on SMP systems. |
74. | Modify grepMT, Program 7-1, so that it puts out the results in the same order as that of the files on the command line. Does this affect the performance measurements in any way? |
75. | Further enhance grepMT, Program 7-1, so that it prints the time required by each worker thread. GetThreadTimes will be required, and this function is similar to GetProcessTimes, which was used in Chapter 6. This enhancement will work only on Windows NT4 and later. |
76. | The book's Web site includes a multithreaded word count program, wcMT.c, that has a structure similar to that of grepMT.c. A defective version, wcMTx.c, is also included. Without referring to the correct solution, analyze and fix the defects in wcMTx.c, including any syntax errors. Also, create test cases that illustrate these defects and carry out performance experiments similar to those suggested for grepMT. There is also a single-threaded version, wcST.c, that can be used to determine whether threads give performance advantages over sequential processing. |
77. | The Web site includes grepMTx.c, which is defective because it violates basic rules for thread safety. Describe the failure symptoms, identify the errors, and fix them. |
78. | SortMT requires that the number of records in the array to be sorted be divisible by the number of threads and that the number of threads be a power of 2. Remove these restrictions. |
79. | Enhance sortMT so that if the number of threads specified on the command line is zero, the program will determine the number of processors on the host system using GetSystemInfo. Set the number of threads to different multiples (1, 2, 4, and so on) of the number of processors and determine the effect on performance. |
710. | Modify sortMT so that the worker threads are not suspended when they are created. What failure symptoms, if any, does the program demonstrate as a result of the race condition defect? |
711. | SortMT reads the entire file in the primary thread before creating the sorting threads. Modify the program so that each thread reads the portion of the file that it requires. |
712. | Modify one of the two programs in this chapter (grepMT or sortMT) so that some or all of the thread-specific information is passed through TLS rather than through a data structure. |
713. | Is there any performance benefit if you give some of the threads in sortMT higher priority than others? For example, it might be beneficial to give the threads that only sort and do not merge, such as Thread 3 in Figure 7-2, a higher priority. Explain the results. |
714. | SortMT creates all the threads in a suspended state so as to avoid a race condition. Modify the program so that it creates the threads in reverse order and in a running state. Are there any remaining race conditions? Compare performance with the original version. |
715. | Quicksort, the algorithm generally used by the C library qsort function, is usually fast, but it can be slow in certain cases. Most texts on algorithms show a version that is fastest when the array is reverse sorted and slowest when it is already sorted. The Microsoft C library implementation is different. Determine from the library code which sequences will produce the best and worst behavior, and study sortMT's performance in these extreme cases. What is the effect of increasing or decreasing the number of threads? Note: The C library source code can be installed in the CRT directory under your Visual Studio installation. Look for qsort.c. Alternatively, you can find it on the installation CD. |
716. | The Web site contains a defective sortMTx.c program. Demonstrate the defects with test cases and then explain and fix the defects without reference to the correct solutions. Caution: The defective version may have syntax errors as well as errors in the thread logic. |
717. | Read "Waiting for More than 64 Objects" by Jason Clark in the October 1997 Windows Developer's Journal. Apply that solution to grepMT. Older journal issues can be difficult to find, so an on-line search with your favorite search engine should locate several items. I used the search phrase "wait for multiple objects more than 64," although other searches may be more effective. |