multithreaded applications. what is multithreaded programming? having your software appear to...
TRANSCRIPT
![Page 1: MultiThreaded Applications. What is Multithreaded Programming? Having your software appear to perform multiple tasks in parallel –Individual paths of](https://reader036.vdocument.in/reader036/viewer/2022082820/56649ea35503460f94ba8258/html5/thumbnails/1.jpg)
MultiThreaded ApplicationsMultiThreaded Applications
![Page 2: MultiThreaded Applications. What is Multithreaded Programming? Having your software appear to perform multiple tasks in parallel –Individual paths of](https://reader036.vdocument.in/reader036/viewer/2022082820/56649ea35503460f94ba8258/html5/thumbnails/2.jpg)
What is Multithreaded What is Multithreaded Programming?Programming?
• Having your software appear to perform multiple tasks in parallel– Individual paths of execution– Threads live in 1 process– Increases parallelization (blocking I/O)– Simplifies design– Better use of CPU– Bad if thread work is small (context
switch)
• Necessary for building servers
![Page 3: MultiThreaded Applications. What is Multithreaded Programming? Having your software appear to perform multiple tasks in parallel –Individual paths of](https://reader036.vdocument.in/reader036/viewer/2022082820/56649ea35503460f94ba8258/html5/thumbnails/3.jpg)
Thread BasicsThread BasicsA re-hash of 2601A re-hash of 2601
• Thread scheduling– Round-robin– Time slicing (each process gets a
turn)– Context-switch– Thread states
•Running•Ready•Blocked
![Page 4: MultiThreaded Applications. What is Multithreaded Programming? Having your software appear to perform multiple tasks in parallel –Individual paths of](https://reader036.vdocument.in/reader036/viewer/2022082820/56649ea35503460f94ba8258/html5/thumbnails/4.jpg)
Thread BasicsThread Basics
• Thread Priorities– REALTIME– HIGH– NORMAL– IDLE
• The CPU will try and run the higher priorities
• Can lead to starvation
![Page 5: MultiThreaded Applications. What is Multithreaded Programming? Having your software appear to perform multiple tasks in parallel –Individual paths of](https://reader036.vdocument.in/reader036/viewer/2022082820/56649ea35503460f94ba8258/html5/thumbnails/5.jpg)
Multiprogramming ConceptsMultiprogramming Concepts
• Atomic operations– Needed for data integrity– Having multiple ASM statements as one– Can't be interrupted by CPU switch
• Mutual Exclusion– Allows programmer to perform a series of
instructions in an atomic manner.
• Race Conditions– Having two or more processes compete for
the same resources
![Page 6: MultiThreaded Applications. What is Multithreaded Programming? Having your software appear to perform multiple tasks in parallel –Individual paths of](https://reader036.vdocument.in/reader036/viewer/2022082820/56649ea35503460f94ba8258/html5/thumbnails/6.jpg)
Multiprogramming ConceptsMultiprogramming Concepts
• Starvation– When one thread never gets service
(I.e. low priority)– Usually because another process is a
hog
![Page 7: MultiThreaded Applications. What is Multithreaded Programming? Having your software appear to perform multiple tasks in parallel –Individual paths of](https://reader036.vdocument.in/reader036/viewer/2022082820/56649ea35503460f94ba8258/html5/thumbnails/7.jpg)
Multiprogramming ConceptsMultiprogramming Concepts
• Semaphores– Used when there are multiple
resources that are the same.– When thread wants resource, takes it
and decrements semaphore.– When finished, returns resource and
increments semaphore– If at 0, fall asleep.
• Is woken up when a resource is free•THEN decrements back to 0
![Page 8: MultiThreaded Applications. What is Multithreaded Programming? Having your software appear to perform multiple tasks in parallel –Individual paths of](https://reader036.vdocument.in/reader036/viewer/2022082820/56649ea35503460f94ba8258/html5/thumbnails/8.jpg)
DeadlockDeadlock
• When threads can't make progress• "I have the bat and want the ball;
you have the ball and want the bat"• Most operating systems simply
ignore deadlock
![Page 9: MultiThreaded Applications. What is Multithreaded Programming? Having your software appear to perform multiple tasks in parallel –Individual paths of](https://reader036.vdocument.in/reader036/viewer/2022082820/56649ea35503460f94ba8258/html5/thumbnails/9.jpg)
Priority InversionPriority Inversion
• Assume A higher priority than B– B has mutex that A needs
• Assume A has highest, B middle and C lowest.– C acquires mutex that is shared with A– Thread B (higher than C) becomes
ready to run– Thread B begins to run– A becomes ready…
![Page 10: MultiThreaded Applications. What is Multithreaded Programming? Having your software appear to perform multiple tasks in parallel –Individual paths of](https://reader036.vdocument.in/reader036/viewer/2022082820/56649ea35503460f94ba8258/html5/thumbnails/10.jpg)
Win32 MultithreadingWin32 Multithreading
• To create a thread in Win32:– Determine security attributes (NULL for
default)– Determine it's stack size (0 for default)– Have a function(s) that can be used by a
thread– The option to pass 1 parameter to that
function– Special flags if you want it suspended (or 0
for default)– A pointer to a DWORD for the thread ID
![Page 11: MultiThreaded Applications. What is Multithreaded Programming? Having your software appear to perform multiple tasks in parallel –Individual paths of](https://reader036.vdocument.in/reader036/viewer/2022082820/56649ea35503460f94ba8258/html5/thumbnails/11.jpg)
Threaded Function DefinitionThreaded Function Definition
• Must look similar to below:
DWORD WINAPI threadFunc (LPVOID par);
![Page 12: MultiThreaded Applications. What is Multithreaded Programming? Having your software appear to perform multiple tasks in parallel –Individual paths of](https://reader036.vdocument.in/reader036/viewer/2022082820/56649ea35503460f94ba8258/html5/thumbnails/12.jpg)
Useful functionsUseful functions
• DWORD tID = GetCurrentThreadId ( );– Determines the thread ID for the
current thread you are in– Good for inside the threaded function– Good for outside the threaded
function to determine primary thread's ID
![Page 13: MultiThreaded Applications. What is Multithreaded Programming? Having your software appear to perform multiple tasks in parallel –Individual paths of](https://reader036.vdocument.in/reader036/viewer/2022082820/56649ea35503460f94ba8258/html5/thumbnails/13.jpg)
Suspend and ResumeSuspend and Resume
• To pause or resume a thread, you only need its handle:DWORD SuspendThread (HANDLE hThread);
DWORD ResumeThread (HANDLE hThread);
![Page 14: MultiThreaded Applications. What is Multithreaded Programming? Having your software appear to perform multiple tasks in parallel –Individual paths of](https://reader036.vdocument.in/reader036/viewer/2022082820/56649ea35503460f94ba8258/html5/thumbnails/14.jpg)
#include <windows.h>#include <stdio.h>
DWORD WINAPI WorkerThreadProc (LPVOID lpThreadParameter);
int main (void) {HANDLE h1, h2, h3;DWORD id1, id2, id3;h1 = CreateThread (NULL, 0, WorkerThreadProc, (LPVOID)5000, 0, &id1);h2 = CreateThread (NULL, 0, WorkerThreadProc, (LPVOID)5000, 0, &id2);h3 = CreateThread (NULL, 0, WorkerThreadProc, (LPVOID)5000, 0, &id3);
if (h1 != NULL) {Sleep (6000);printf ("Done");CloseHandle(h1); CloseHandle (h2); CloseHandle (h3);
}else {
printf ("Failed to create worker thread: error 0x%x\n", GetLastError());}return 0;
}
DWORD WINAPI WorkerThreadProc (LPVOID lpThreadParameter, LPVOID par2) {DWORD dwNumSeconds = (DWORD)lpThreadParameter;DWORD dwThreadId = GetCurrentThreadId();for (int i = 0; i < (int)dwNumSeconds; i++) {
printf ("[%08x] Hello, multithreaded world! = %d\n", dwThreadId, i);}return (dwThreadId);
}
![Page 15: MultiThreaded Applications. What is Multithreaded Programming? Having your software appear to perform multiple tasks in parallel –Individual paths of](https://reader036.vdocument.in/reader036/viewer/2022082820/56649ea35503460f94ba8258/html5/thumbnails/15.jpg)
Now, in JavaNow, in Java
• Java is pure OOP• Class has a run( ) method• Instances are the threads• 2 ways to create Threaded class
– extend Thread– implement Runnable
![Page 16: MultiThreaded Applications. What is Multithreaded Programming? Having your software appear to perform multiple tasks in parallel –Individual paths of](https://reader036.vdocument.in/reader036/viewer/2022082820/56649ea35503460f94ba8258/html5/thumbnails/16.jpg)
class MyThread extends Thread {int threadID;MyThread(int x) {
threadID = x;this.start( ); // Start calls run
}public void run ( ) {
for (int i = 0; i < 5000; i++) {System.out.println (threadID+":"+i);
}// First to 5000 kills the programSystem.exit(1);
}public static void main (String args[ ]) {
MyThread t1 = new MyThread(1);MyThread t2 = new MyThread(2);MyThread t3 = new MyThread(3);
}}
![Page 17: MultiThreaded Applications. What is Multithreaded Programming? Having your software appear to perform multiple tasks in parallel –Individual paths of](https://reader036.vdocument.in/reader036/viewer/2022082820/56649ea35503460f94ba8258/html5/thumbnails/17.jpg)
class MyThread implements Runnable {int threadID;Thread t;MyThread(int x) {
threadID = x;t = new Thread(this);t.start( );
} public void run ( ) {
for (int i = 0; i < 5000; i++) {System.out.println (threadID+":"+i);
}System.exit(1);
}public static void main (String args[ ]) {
MyThread t1 = new MyThread(1);MyThread t2 = new MyThread(2);MyThread t3 = new MyThread(3);
}}
![Page 18: MultiThreaded Applications. What is Multithreaded Programming? Having your software appear to perform multiple tasks in parallel –Individual paths of](https://reader036.vdocument.in/reader036/viewer/2022082820/56649ea35503460f94ba8258/html5/thumbnails/18.jpg)