For i 1 i upper i sum i pthreadexit0 threadwin32c

  • Notes
  • AmbassadorBookCrocodile8329
  • 5
  • 100% (1) 1 out of 1 people found this document helpful

This preview shows page 2 - 5 out of 5 pages.

for (i = 1; i <= upper; i++) sum += i; } pthread_exit(0); } threadWin32.c /** * This program creates a separate thread using the CreateThread() system call. */ #include <stdio.h> #include <windows.h> DWORD Sum; /* data is shared by the thread(s) */ /* the thread runs in this separate function */ DWORD WINAPI Summation(PVOID Param) { DWORD Upper = *(DWORD *)Param; DWORD i; for (i = 0; i <= Upper; i++)
Image of page 2

Subscribe to view the full document.

Sum += i; return 0; } int main(int argc, char *argv[]) { DWORD ThreadId; HANDLE ThreadHandle; int Param; // do some basic error checking 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; } // create the thread ThreadHandle = CreateThread(NULL, 0, Summation, &Param, 0, &ThreadId); if (ThreadHandle != NULL) { WaitForSingleObject(ThreadHandle, INFINITE); CloseHandle(ThreadHandle); printf("sum = %d\n",Sum); } } summationThread.java /** * This program creates a separate thread by implementing the Runnable interface. * */
Image of page 3
class Sum { private int sum; public int get() { return sum; } public void set(int sum) { this.sum = sum; } } class Summation implements Runnable { private int upper; private Sum sumValue; public Summation(int upper, Sum sumValue) { if (upper < 0) throw new IllegalArgumentException(); this.upper = upper; this.sumValue = sumValue; } public void run() { int sum = 0; for (int i = 0; i <= upper; i++) sum += i; sumValue.set(sum); } } public class Driver { public static void main(String[] args) { if (args.length != 1) { System.err.println("Usage Driver <integer>"); System.exit(0); }
Image of page 4

Subscribe to view the full document.

Sum sumObject = new Sum(); int upper = Integer.parseInt(args[0]); Thread worker = new Thread(new Summation(upper, sumObject)); worker.start(); try { worker.join(); } catch (InterruptedException ie) { } System.out.println("The sum of " + upper + " is " + sumObject.get()); } }
Image of page 5
You've reached the end of this preview.
  • Spring '12
  • GwangS.Jung
  • Addition, Summation, int sum

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern