1 / 13

Exercise #3: Multithreaded Programming

Exercise #3: Multithreaded Programming. J. H. Wang Apr. 13, 2010. Objectives. To get you familiar with multithreaded program development To learn how to create and terminate a thread in Pthreads, Win32 Thread, and Java Threads. Example in Pthreads.

verlee
Download Presentation

Exercise #3: Multithreaded Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Exercise #3: Multithreaded Programming J. H. Wang Apr. 13, 2010

  2. Objectives • To get you familiar with multithreaded program development • To learn how to create and terminate a thread in Pthreads, Win32 Thread, and Java Threads

  3. Example in Pthreads • #include <pthread.h>#include <stdio.h>in sum;void *runner(void *param);int main(int argc, char *argv[]){ pthread_t tid; pthread_attr_t attr; if (argc != 2) { fprintf(stderr, “usage: a.out <integer value>\n”); return -1; } if (atoi(argv[1])>0) { fprintf(stderr, “%d must be >= 0\n”, atoi(argv[1])); return -1; } pthread_attr_init(&attr); ? (&tid, &attr, runner, argv[1]); ? (tid, NULL); printf(“sum = %d\n”, sum);}

  4. void *runner(void *param){ int i, upper = atoi(param); sum = 0; for (i=1; i<=upper; i++) sum += i; ? (0);}

  5. Example in Win32 Thread • #include <windows.h>#include <stdio.h>DWORD Sum;DWORD WINAPI Summation(LPVOID Param){ DWORD Upper = *(DWORD*)Param; for (DWORD i=0; i<=Upper; i++) Sum += i; return 0; }int main(int argc, char *argv[]){ DWORD ThreadId; HANDLE ThreadHandle; int Param; if (argc != 2) { fprintf(stderr, “An integer parameter is required\n”); return -1; } Param = atoi(argv[1]); if (Param<0) { fprintf(stderr, “An integer >=0 is required\n”); return -1; }

  6. ThreadHandle = ? (NULL, 0, Summation, &Param, 0, &ThreadId); if (ThreadHandle !=NULL) { ? (ThreadHandle, INFINITE);CloseHandle(ThreadHandle); printf(“sum = %d\n”, Sum); } }

  7. Example in Java Threads • class Sum{ private int sum; public int getSum() { return sum; } public void setSum(int sum) { this.sum = sum; }}class Summation implements Runnable{ private int upper; private Sum sumValue; public Summation (int upper, Sum sumValue) { this.upper = upper; this.sumValue = sumValue; } public void run() { int sum = 0; for (int i=0; i<=upper; i++) sum += i; sumValue.setSum(sum); }}

  8. public class Driver{ public static void main(String[] args) { if (args.length > 0) { if (Integer.parseInt(args[0]) < 0) System.err.println(args[0] + “ must be >=0.”); else { Sum sumObject = new Sum(); int upper = Integer.parseInt(args[0]); Thread thrd = new Thread(new Summation(upper, sumObject)); thrd.start(); try { thrd.join(); System.out.println(“The sum of “ +upper+ “ is “+sumObject.getSum()); } catch (InterruptedException ie) {} } } else System.err.println( “Usage: Summation <integer value>.”); }}

  9. Pthread Scheduling API #include <pthread.h> #include <stdio.h> #define NUM_THREADS 5 int main(int argc, char *argv[]) { int i; pthread t tid[NUM_THREADS]; pthread_attr t_attr; /* get the default attributes */ pthread_attr_init(&attr); /* set the scheduling algorithm to PROCESS or SYSTEM*/ ? (&attr, PTHREAD_SCOPE_SYSTEM); /* set the scheduling policy - FIFO, RT, or OTHER */ pthread_attr_setschedpolicy(&attr, SCHED_OTHER);

  10. /* create the threads */ for (i = 0; i < NUM_THREADS; i++) pthread_create(&tid[i],&attr,runner,NULL); /* now join on each thread */ for (i = 0; i < NUM_THREADS; i++) pthread_join(tid[i], NULL); } /* Each thread will begin control in this function */ void *runner(void *param) { printf("I am a thread\n"); pthread_exit(0); }

  11. Exercises • Ex. 4.12, 4.13

  12. Further Reading • End-of-Chapter Projects in Chap. 4 • Project 1: Naming Service Project • The Server • The Client • Project 2: Matrix Multiplication Project • Passing parameters to each thread • Waiting for threads to complete

More Related