Presentation is loading. Please wait.

Presentation is loading. Please wait.

MultiThreaded Applications. What is Multithreaded Programming? Having your software appear to perform multiple tasks in parallel –Individual paths of.

Similar presentations


Presentation on theme: "MultiThreaded Applications. What is Multithreaded Programming? Having your software appear to perform multiple tasks in parallel –Individual paths of."— Presentation transcript:

1 MultiThreaded Applications

2 What is Multithreaded 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

3 Thread Basics A re-hash of 2601 Thread scheduling –Round-robin –Time slicing (each process gets a turn) –Context-switch –Thread states Running Ready Blocked

4 Thread Basics Thread Priorities –REALTIME –HIGH –NORMAL –IDLE The CPU will try and run the higher priorities Can lead to starvation

5 Multiprogramming 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

6 Multiprogramming Concepts Starvation –When one thread never gets service (I.e. low priority) –Usually because another process is a hog

7 Multiprogramming 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

8 Deadlock 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

9 Priority 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…

10 Win32 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

11 Threaded Function Definition Must look similar to below: DWORD WINAPI threadFunc (LPVOID par);

12 Useful 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

13 Suspend and Resume To pause or resume a thread, you only need its handle: DWORD SuspendThread (HANDLE hThread); DWORD ResumeThread (HANDLE hThread);

14 #include 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); }

15 Now, 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

16 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 program System.exit(1); } public static void main (String args[ ]) { MyThread t1 = new MyThread(1); MyThread t2 = new MyThread(2); MyThread t3 = new MyThread(3); }

17 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); }

18


Download ppt "MultiThreaded Applications. What is Multithreaded Programming? Having your software appear to perform multiple tasks in parallel –Individual paths of."

Similar presentations


Ads by Google