windows application development
DESCRIPTION
Windows Application Development. Chapter 7 Windows Thread Management. Objectives and Benefits. Upon completion of this chapter , you will be able to: Describe Windows thread management Use threads in Windows applications Use threads with C library functions - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Windows Application Development](https://reader035.vdocuments.us/reader035/viewer/2022062521/56814a42550346895db75cee/html5/thumbnails/1.jpg)
1-1© 2004 JMH Associates. All rights reserved.
Windows Application DevelopmentWindows Application Development
Chapter 7Windows Thread Management
![Page 2: Windows Application Development](https://reader035.vdocuments.us/reader035/viewer/2022062521/56814a42550346895db75cee/html5/thumbnails/2.jpg)
1-2© 2004 JMH Associates. All rights reserved.
Objectives and BenefitsObjectives and Benefits
Upon completion of this chapter, you will be able to: Describe Windows thread management Use threads in Windows applications Use threads with C library functions Build and execute threaded applications Describe scheduling and SMP operation
![Page 3: Windows Application Development](https://reader035.vdocuments.us/reader035/viewer/2022062521/56814a42550346895db75cee/html5/thumbnails/3.jpg)
1-3© 2004 JMH Associates. All rights reserved.
Threads: Benefits and RisksThreads: Benefits and Risks
Benefits Simpler program models Faster code – in many cases
Exploit multiple processors Exploit inherent application parallelism
Reliable, understandable, maintainable code Risks
Slower performance – in some cases Potential defects
![Page 4: Windows Application Development](https://reader035.vdocuments.us/reader035/viewer/2022062521/56814a42550346895db75cee/html5/thumbnails/4.jpg)
1-4© 2004 JMH Associates. All rights reserved.
ContentsContents
1. Process and Thread Overview2. Thread Management3. Waiting for Thread Termination4. The C Library and Threads5. Demonstration: Building a Threaded
Application6. Thread Priority, Scheduling, SMP
Impact7. Lab Exercise 7-1
![Page 5: Windows Application Development](https://reader035.vdocuments.us/reader035/viewer/2022062521/56814a42550346895db75cee/html5/thumbnails/5.jpg)
1-5© 2004 JMH Associates. All rights reserved.
1. Process and Thread Overview1. Process and Thread Overview
Threads in a process share data and code Each thread has its own stack for function calls Calling thread can pass an argument to a
thread at creation time This argument is on the stack
Each thread can allocate its own Thread Local Storage (TLS) indices and set TLS values
![Page 6: Windows Application Development](https://reader035.vdocuments.us/reader035/viewer/2022062521/56814a42550346895db75cee/html5/thumbnails/6.jpg)
1-6© 2004 JMH Associates. All rights reserved.
Process and Thread OverviewProcess and Thread Overview
Threads are scheduled and run independently The executive schedules threads Threads run asynchronously Threads can be preempted
Or restarted at any time
![Page 7: Windows Application Development](https://reader035.vdocuments.us/reader035/viewer/2022062521/56814a42550346895db75cee/html5/thumbnails/7.jpg)
1-7© 2004 JMH Associates. All rights reserved.
Processes and ThreadsProcesses and ThreadsProcess
Code
Global Variables
Process Heap
Process ResourcesOpen Files
Heaps…
Environment Block
...
Thread 1
Thread Local Storage
Stack
Thread N
Thread Local Storage
Stack
![Page 8: Windows Application Development](https://reader035.vdocuments.us/reader035/viewer/2022062521/56814a42550346895db75cee/html5/thumbnails/8.jpg)
1-8© 2004 JMH Associates. All rights reserved.
Read File B
Single-Threaded Program Multithreaded Program
Read File A
Read File B
Reading File B before
File A would give the same results
Read File A
Wait forThread 1 and
Thread 2 to finish
Merge datafrom both files
Thread 1
Thread 3
Thread 2
Thread 3
Merge datafrom both files
Threads Performing Parallel TasksThreads Performing Parallel Tasks
![Page 9: Windows Application Development](https://reader035.vdocuments.us/reader035/viewer/2022062521/56814a42550346895db75cee/html5/thumbnails/9.jpg)
1-9© 2004 JMH Associates. All rights reserved.
2. Thread Management2. Thread Management
Creating a Thread The Thread Function Thread Termination Thread Exit Codes Thread Identities Suspending and
Resuming Threads
![Page 10: Windows Application Development](https://reader035.vdocuments.us/reader035/viewer/2022062521/56814a42550346895db75cee/html5/thumbnails/10.jpg)
1-10© 2004 JMH Associates. All rights reserved.
Creating a Thread (1 of 6)Creating a Thread (1 of 6)
Specify the thread’s start address within the process’ code
Specify the stack size, and the stack consumes space within the process’ address space The stack cannot be expanded
![Page 11: Windows Application Development](https://reader035.vdocuments.us/reader035/viewer/2022062521/56814a42550346895db75cee/html5/thumbnails/11.jpg)
1-11© 2004 JMH Associates. All rights reserved.
Creating a Thread (2 of 6)Creating a Thread (2 of 6)
Specify a pointer to an argument for the thread Can be nearly anything Interpreted by the thread itself
CreateThread returns a thread’s ID value and its handle A NULL handle value indicates failure
![Page 12: Windows Application Development](https://reader035.vdocuments.us/reader035/viewer/2022062521/56814a42550346895db75cee/html5/thumbnails/12.jpg)
1-12© 2004 JMH Associates. All rights reserved.
Creating a Thread (3 of 6)Creating a Thread (3 of 6)
HANDLE CreateThread (LPSECURITY_ATTRIBUTES lpsa,DWORD cbStack,LPTHREAD_START_ROUTINE lpStartAddr,LPVOID lpvThreadParm,DWORD dwCreate,LPDWORD lpIDThread )
![Page 13: Windows Application Development](https://reader035.vdocuments.us/reader035/viewer/2022062521/56814a42550346895db75cee/html5/thumbnails/13.jpg)
1-13© 2004 JMH Associates. All rights reserved.
Creating a Thread (4 of 6)Creating a Thread (4 of 6)
Parameterslpsa Security attributes structure (use NULL)
cbStack Byte size for the new thread’s stack Use 0 to default to the primary thread’s stack
size (1 MB)
![Page 14: Windows Application Development](https://reader035.vdocuments.us/reader035/viewer/2022062521/56814a42550346895db75cee/html5/thumbnails/14.jpg)
1-14© 2004 JMH Associates. All rights reserved.
Creating a Thread (5 of 6)Creating a Thread (5 of 6)
lpStartAddr Points to the function (within the calling
process) to be executed Accepts a single pointer argument and returns
a 32-bit DWORD exit code The thread can interpret the argument as a DWORD or a pointer
lpThreadParm The pointer passed as the thread argument
![Page 15: Windows Application Development](https://reader035.vdocuments.us/reader035/viewer/2022062521/56814a42550346895db75cee/html5/thumbnails/15.jpg)
1-15© 2004 JMH Associates. All rights reserved.
Creating a Thread (6 of 6)Creating a Thread (6 of 6)
dwCreate If zero, the thread is immediately ready to run If CREATE_SUSPENDED, the new thread will be
in the suspended state, requiring a ResumeThread function call to move the thread to the ready state
lpIDThread Points to a DWORD that receives the new
thread’s identifier; NULL OK on W2000/NT
![Page 16: Windows Application Development](https://reader035.vdocuments.us/reader035/viewer/2022062521/56814a42550346895db75cee/html5/thumbnails/16.jpg)
1-16© 2004 JMH Associates. All rights reserved.
The Thread FunctionThe Thread Function
DWORD WINAPI MyThreadFunc (PVOID pThParam )
{ . . .ExitThread (ExitCode); /* OR */return ExitCode;
}
![Page 17: Windows Application Development](https://reader035.vdocuments.us/reader035/viewer/2022062521/56814a42550346895db75cee/html5/thumbnails/17.jpg)
1-17© 2004 JMH Associates. All rights reserved.
Thread Termination (1 of 3)Thread Termination (1 of 3)
Threads are terminated by ExitProcess The process and all its threads terminate The exit code returned by the thread start
function same as the process exit code Or a thread can simply return with its exit code
![Page 18: Windows Application Development](https://reader035.vdocuments.us/reader035/viewer/2022062521/56814a42550346895db75cee/html5/thumbnails/18.jpg)
1-18© 2004 JMH Associates. All rights reserved.
Thread Termination (2 of 3)Thread Termination (2 of 3)
ExitThread is the preferred technique The thread’s stack is deallocated on termination
VOID ExitThread (DWORD (dwExitCode)
When the last thread in a process terminates, so does the process itself
![Page 19: Windows Application Development](https://reader035.vdocuments.us/reader035/viewer/2022062521/56814a42550346895db75cee/html5/thumbnails/19.jpg)
1-19© 2004 JMH Associates. All rights reserved.
Thread Termination (3 of 3)Thread Termination (3 of 3)
You can terminate a different thread with TerminateThread Dangerous: The thread’s stack and other
resources will not be deallocated Better to let the thread terminate itself
A thread will remain in the system until the last handle to it is closed (using CloseHandle) Then the thread will be deleted
Any other thread can retrieve the exit code
![Page 20: Windows Application Development](https://reader035.vdocuments.us/reader035/viewer/2022062521/56814a42550346895db75cee/html5/thumbnails/20.jpg)
1-20© 2004 JMH Associates. All rights reserved.
Thread Exit CodesThread Exit Codes
BOOL GetExitCodeThread (HANDLE hThread,LPDWORD lpdwExitCode )
lpdwExitCode Contains the thread’s exit code It could be STILL_ACTIVE
![Page 21: Windows Application Development](https://reader035.vdocuments.us/reader035/viewer/2022062521/56814a42550346895db75cee/html5/thumbnails/21.jpg)
1-21© 2004 JMH Associates. All rights reserved.
Thread Identities (1 of 2)Thread Identities (1 of 2)
A thread has a permanent “ThreadId” A thread is usually accessed by HANDLE An ID can be converted to a HANDLE
![Page 22: Windows Application Development](https://reader035.vdocuments.us/reader035/viewer/2022062521/56814a42550346895db75cee/html5/thumbnails/22.jpg)
1-22© 2004 JMH Associates. All rights reserved.
Thread Identities (2 of 2)Thread Identities (2 of 2)
HANDLE GetCurrentThread (VOID);DWORD GetCurrentThreadId (VOID);HANDLE OpenThread (
DWORD dwDesiredAccess,BOOL InheritableHandle,DWORD ThreadId );
/* >= Windows 2000 only */
![Page 23: Windows Application Development](https://reader035.vdocuments.us/reader035/viewer/2022062521/56814a42550346895db75cee/html5/thumbnails/23.jpg)
1-23© 2004 JMH Associates. All rights reserved.
Suspend & Resume Threads (1 of 2)Suspend & Resume Threads (1 of 2)
Every thread has a suspend count A thread can execute only if this count is zero
A thread can be created in the suspended state One thread can increment or decrement the
suspend count of another:
DWORD ResumeThread (HANDLE hThread)
![Page 24: Windows Application Development](https://reader035.vdocuments.us/reader035/viewer/2022062521/56814a42550346895db75cee/html5/thumbnails/24.jpg)
1-24© 2004 JMH Associates. All rights reserved.
Suspend & Resume Threads (2 of 2)Suspend & Resume Threads (2 of 2)
DWORD SuspendThread (HANDLE hThread)
Both functions return previous suspend count 0xFFFFFFFF indicates failure Useful in preventing “race conditions”
Do not allow threads to start until initialization is complete
Unsafe for general synchronization
![Page 25: Windows Application Development](https://reader035.vdocuments.us/reader035/viewer/2022062521/56814a42550346895db75cee/html5/thumbnails/25.jpg)
1-25© 2004 JMH Associates. All rights reserved.
3. Waiting for Thread Termination3. Waiting for Thread Termination
Wait for a thread to terminate using general purpose wait functions
WaitForSingleObject or WaitForMultipleObjects Using thread handles
The wait functions wait for the thread handle to become signaled Thread handle is signaled when thread
terminates
![Page 26: Windows Application Development](https://reader035.vdocuments.us/reader035/viewer/2022062521/56814a42550346895db75cee/html5/thumbnails/26.jpg)
1-26© 2004 JMH Associates. All rights reserved.
Waiting for Thread Termination (2 of 2)Waiting for Thread Termination (2 of 2)
ExitThread and TerminateThread set the object to the signaled state Releasing all other threads waiting on the
object ExitProcess sets the process’ state and all
its threads’ states to signaled
![Page 27: Windows Application Development](https://reader035.vdocuments.us/reader035/viewer/2022062521/56814a42550346895db75cee/html5/thumbnails/27.jpg)
1-27© 2004 JMH Associates. All rights reserved.
The Wait Functions (1 of 2)The Wait Functions (1 of 2)
DWORD WaitForSingleObject (HANDLE hObject,DWORD dwTimeOut )
![Page 28: Windows Application Development](https://reader035.vdocuments.us/reader035/viewer/2022062521/56814a42550346895db75cee/html5/thumbnails/28.jpg)
1-28© 2004 JMH Associates. All rights reserved.
The Wait Functions (2 of 2)The Wait Functions (2 of 2)
DWORD WaitForMultipleObjects (DWORD cObjects,LPHANDLE lphObjects,BOOL fWaitAll,DWORD dwTimeOut )
Return: The cause of the wait completion
![Page 29: Windows Application Development](https://reader035.vdocuments.us/reader035/viewer/2022062521/56814a42550346895db75cee/html5/thumbnails/29.jpg)
1-29© 2004 JMH Associates. All rights reserved.
Wait Options (1 of 2)Wait Options (1 of 2)
Specify either a single handle hObject
Or an array of cObjects referenced by lphObjects
cObjects should not exceed MAXIMUM_WAIT_OBJECTS - 64
![Page 30: Windows Application Development](https://reader035.vdocuments.us/reader035/viewer/2022062521/56814a42550346895db75cee/html5/thumbnails/30.jpg)
1-30© 2004 JMH Associates. All rights reserved.
Wait Options (2 of 2)Wait Options (2 of 2)
dwTimeOut is in milliseconds 0 means the function returns immediately after
testing the state of the specified objects Use INFINITE for no timeout
Wait forever for a thread to terminate GetExitCodeThread
Returns the thread exit code
![Page 31: Windows Application Development](https://reader035.vdocuments.us/reader035/viewer/2022062521/56814a42550346895db75cee/html5/thumbnails/31.jpg)
1-31© 2004 JMH Associates. All rights reserved.
Wait Function Return Values (1 of 3)Wait Function Return Values (1 of 3)
fWaitAll If TRUE, wait for all threads to terminate
Possible return values are: WAIT_OBJECT_0
The thread terminated (if calling WaitForMultipleObjects; fWaitAll set)
![Page 32: Windows Application Development](https://reader035.vdocuments.us/reader035/viewer/2022062521/56814a42550346895db75cee/html5/thumbnails/32.jpg)
1-32© 2004 JMH Associates. All rights reserved.
Wait Function Return Values (2 of 3)Wait Function Return Values (2 of 3)
WAIT_OBJECT_0 + nwhere 0 <= n < cObjects
Subtract WAIT_OBJECT_0 from the return value to determine which thread terminated when calling WaitForMultipleObjects with fWaitAll set
WAIT_TIMEOUT Timeout period elapsed
![Page 33: Windows Application Development](https://reader035.vdocuments.us/reader035/viewer/2022062521/56814a42550346895db75cee/html5/thumbnails/33.jpg)
1-33© 2004 JMH Associates. All rights reserved.
Wait Function Return Values (3 of 3)Wait Function Return Values (3 of 3)
WAIT_ABANDONED Not possible with thread handles
WAIT_FAILED Call GetLastError for thread-specific error code
![Page 34: Windows Application Development](https://reader035.vdocuments.us/reader035/viewer/2022062521/56814a42550346895db75cee/html5/thumbnails/34.jpg)
1-34© 2004 JMH Associates. All rights reserved.
4. The C Library and Threads4. The C Library and Threads
Nearly all programs (and thread functions) use the C library
But the normal C library is not “thread safe”
The C function _beginthreadex has exactly the same parameters as CreateThread
![Page 35: Windows Application Development](https://reader035.vdocuments.us/reader035/viewer/2022062521/56814a42550346895db75cee/html5/thumbnails/35.jpg)
1-35© 2004 JMH Associates. All rights reserved.
Using Using _beginthreadex_beginthreadex (1 of 3) (1 of 3)
Cast the _beginthreadex return value to (HANDLE)
Use _endthreadex in place of ExitThread
#include <process.h>
![Page 36: Windows Application Development](https://reader035.vdocuments.us/reader035/viewer/2022062521/56814a42550346895db75cee/html5/thumbnails/36.jpg)
1-36© 2004 JMH Associates. All rights reserved.
Using Using _beginthreadex_beginthreadex (2 of 3) (2 of 3)
Set the multithreaded environment as follows: #define _MT in every source file before <windows.h>
Link with LIBCMT.LIB Override the default library
![Page 37: Windows Application Development](https://reader035.vdocuments.us/reader035/viewer/2022062521/56814a42550346895db75cee/html5/thumbnails/37.jpg)
1-37© 2004 JMH Associates. All rights reserved.
Using Using _beginthreadex_beginthreadex (3 of 3) (3 of 3)
Preferred method using Visual C++ From the menu bar:
Build Settings — C/C++ Tab Code Generation category Select a multithreaded run-time library
![Page 38: Windows Application Development](https://reader035.vdocuments.us/reader035/viewer/2022062521/56814a42550346895db75cee/html5/thumbnails/38.jpg)
1-38© 2004 JMH Associates. All rights reserved.
Ex: A Simple Boss ThreadEx: A Simple Boss ThreadHANDLE hWork[K];volatile LONGLONG WorkDone[K], iTh; /* !! */
. . .for (iTh = 0; iTh < K; iTh++) {
WorkDone[ith] = 0;hWork[iTh] = _beginthreadex (NULL, 0,WorkTh, (PVOID)&iTh, 0, NULL); /* BUG! */
}WaitForMultipleObjects (K, hWork, TRUE,
INFINITE);for (iTh = 0; iTh < K; iTh++)
printf (“Thread %d did %d workunits\n”,iTh, WorkDone[iTh]);
![Page 39: Windows Application Development](https://reader035.vdocuments.us/reader035/viewer/2022062521/56814a42550346895db75cee/html5/thumbnails/39.jpg)
1-39© 2004 JMH Associates. All rights reserved.
Ex: A Simple Worker ThreadEx: A Simple Worker Thread
DWORD WINAPI WorkTh (PVOID pThNum){
DWORD ThNum = (DWORD)(*pThNum);while (. . .) {
/* Perform work */WorkDone[ThNum]++;
}_endthreadex (0);
}
![Page 40: Windows Application Development](https://reader035.vdocuments.us/reader035/viewer/2022062521/56814a42550346895db75cee/html5/thumbnails/40.jpg)
1-40© 2004 JMH Associates. All rights reserved.
5. Demonstration: Building a Threaded 5. Demonstration: Building a Threaded ApplicationApplication
Using Visual C++ Ver. 6.0, 7.0
![Page 41: Windows Application Development](https://reader035.vdocuments.us/reader035/viewer/2022062521/56814a42550346895db75cee/html5/thumbnails/41.jpg)
1-41© 2004 JMH Associates. All rights reserved.
Demonstration Demonstration (1 of 2)(1 of 2)
Implement multithreaded word count programwcMT
Include the source code of wc with your application and execute it as a thread
Create one thread for each file to analyze Compare performance of single process, multi-
ple process, & multiple thread implementations If you use other libraries, build thread-safe
versions now!
![Page 42: Windows Application Development](https://reader035.vdocuments.us/reader035/viewer/2022062521/56814a42550346895db75cee/html5/thumbnails/42.jpg)
1-42© 2004 JMH Associates. All rights reserved.
DemonstrationDemonstration (2 of 2) (2 of 2)
THIS IS A BOSS/WORKER SYSTEM Shows common thread management
techniques See picture on the next overhead
CHALLENGE: Also try at wcMTx.c and wcMTxx.c These contains some common bugs
What are the bug symptoms?
![Page 43: Windows Application Development](https://reader035.vdocuments.us/reader035/viewer/2022062521/56814a42550346895db75cee/html5/thumbnails/43.jpg)
1-43© 2004 JMH Associates. All rights reserved.
Boss/Worker Model for Boss/Worker Model for wcMTwcMT
Program Resources
Files
Workers
Thread K
Thread 1
Thread 0
main ()
Boss
![Page 44: Windows Application Development](https://reader035.vdocuments.us/reader035/viewer/2022062521/56814a42550346895db75cee/html5/thumbnails/44.jpg)
1-44© 2004 JMH Associates. All rights reserved.
6.Thread Priority and Scheduling6.Thread Priority and Scheduling
Windows kernels run the highest-priority thread that is ready for execution
4 priority classes set by CreateProcess IDLE_PRIORITY_CLASS (base priority 4) NORMAL_PRIORITY_CLASS (9 or 7) HIGH_PRIORITY_CLASS (13) REALTIME_PRIORITY_CLASS (24)
Windows XP extensions
![Page 45: Windows Application Development](https://reader035.vdocuments.us/reader035/viewer/2022062521/56814a42550346895db75cee/html5/thumbnails/45.jpg)
1-45© 2004 JMH Associates. All rights reserved.
Thread Priority (1 of 2)Thread Priority (1 of 2)
Change or determine a thread’s priority For itself For another process, security permitting
DWORD SetPriorityClass (HANDLE hProcess, DWORD dwPriority)
DWORD GetPriorityClass (HANDLE hProcess)
![Page 46: Windows Application Development](https://reader035.vdocuments.us/reader035/viewer/2022062521/56814a42550346895db75cee/html5/thumbnails/46.jpg)
1-46© 2004 JMH Associates. All rights reserved.
Thread Priority (2 of 2)Thread Priority (2 of 2)
Thread priorities set relative to base priority THREAD_PRIORITY_LOWEST THREAD_PRIORITY_BELOW_NORMAL THREAD_PRIORITY_NORMAL THREAD_PRIORITY_ABOVE_NORMAL THREAD_PRIORITY_HIGHEST
![Page 47: Windows Application Development](https://reader035.vdocuments.us/reader035/viewer/2022062521/56814a42550346895db75cee/html5/thumbnails/47.jpg)
1-47© 2004 JMH Associates. All rights reserved.
Process Priority CautionsProcess Priority Cautions
Use high thread priorities with caution Avoid real time priorities for user processes
User threads may preempt executive threads Assure fairness
All threads should run eventually Real time priorities may prevent fairness
“Priority inversion” “Thread starvation”
![Page 48: Windows Application Development](https://reader035.vdocuments.us/reader035/viewer/2022062521/56814a42550346895db75cee/html5/thumbnails/48.jpg)
1-48© 2004 JMH Associates. All rights reserved.
Thread States and Transitions (1 of 2)Thread States and Transitions (1 of 2)
A thread is running when it is on a processor SMP systems have multiple processors Intel Xeon provides single processor
multiprocessing - Hyperthreading The executive can place a running thread in
the wait state I/O operations wait wait for data transfer The thread is blocked or sleeping
A thread is ready if it could be running
![Page 49: Windows Application Development](https://reader035.vdocuments.us/reader035/viewer/2022062521/56814a42550346895db75cee/html5/thumbnails/49.jpg)
1-49© 2004 JMH Associates. All rights reserved.
Terminated
Running
Initialized
WaitingReady
Create a new thread
Time sliceis over
Goes toready queue
Wait, SleepOr any blocking call
Object issignaled
Thread Id is reused
Scheduled ona free processor
ExitThreadTerminateThread
![Page 50: Windows Application Development](https://reader035.vdocuments.us/reader035/viewer/2022062521/56814a42550346895db75cee/html5/thumbnails/50.jpg)
1-50© 2004 JMH Associates. All rights reserved.
Thread States and Transitions (2 of 3)Thread States and Transitions (2 of 3)
The scheduler can place a ready thread on any available processor
The executive moves a running thread to the ready state if the thread’s time slice expires Sleep(0) moves a running thread to ready
The executive makes a waiting thread ready as soon as appropriate handles are signaled The thread wakes up
![Page 51: Windows Application Development](https://reader035.vdocuments.us/reader035/viewer/2022062521/56814a42550346895db75cee/html5/thumbnails/51.jpg)
1-51© 2004 JMH Associates. All rights reserved.
Thread States and Transitions (3 of 3)Thread States and Transitions (3 of 3)
A thread can be suspended or resumed A thread is in the terminated state after it has
been terminated but there still open handles Other threads may interrogate the thread’s
state and exit code
![Page 52: Windows Application Development](https://reader035.vdocuments.us/reader035/viewer/2022062521/56814a42550346895db75cee/html5/thumbnails/52.jpg)
1-52© 2004 JMH Associates. All rights reserved.
SummarySummary
Threads allow concurrent processing Reducing overall system overhead Potentially simplifying programs
Single-threaded programs can be inefficient Managing concurrent and interacting tasks
Threads can streamline I/O-bound programs There are reliability and performance risks
Design and code carefully
![Page 53: Windows Application Development](https://reader035.vdocuments.us/reader035/viewer/2022062521/56814a42550346895db75cee/html5/thumbnails/53.jpg)
1-53© 2004 JMH Associates. All rights reserved.
8. Lab Exercise 7-18. Lab Exercise 7-1
Implement a multithreaded sort command, sortMT nTh FileName
Use the scheme shown on the next overhead nTh: number of threads If properly implemented, it will transparently
take advantage of SMP systems
![Page 54: Windows Application Development](https://reader035.vdocuments.us/reader035/viewer/2022062521/56814a42550346895db75cee/html5/thumbnails/54.jpg)
1-54© 2004 JMH Associates. All rights reserved.
sortMTsortMT Design DesignArray
qsort
qsort
qsort
qsort
merge
merge
mergeThread0
Thread1
Thread2
Thread3
for (i = 0; i < 4; i++) CreateThread ( )
Wait (Thread0)/* Array is Sorted */
wait (Thread3)
wait (Thread2)wait (Thread1)
![Page 55: Windows Application Development](https://reader035.vdocuments.us/reader035/viewer/2022062521/56814a42550346895db75cee/html5/thumbnails/55.jpg)
1-55© 2004 JMH Associates. All rights reserved.
Lab Exercise (cont’d)Lab Exercise (cont’d)
CAUTION: The lab solution requires that the number of
records in the file to be sorted be a multiple of the number of threads
The number of threads must be a power of two SUGGESTION:
Start with sortMTx.c, a buggy solution The correct solution is also provided
![Page 56: Windows Application Development](https://reader035.vdocuments.us/reader035/viewer/2022062521/56814a42550346895db75cee/html5/thumbnails/56.jpg)
1-56© 2004 JMH Associates. All rights reserved.
Extra Credit — Additional ExerciseExtra Credit — Additional Exercise
Remove the restrictions: The command form is sortMT nTh file nTh must be a power of two The number of records in file must be a
multiple of nTh What is the effect of multiple threads on per-
formance, even on a single processor system? Hint: Test with large files