lab6-integral

lab6-integral - #include #include #include #include #...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
#include <iostream> #include <stdlib.h> #include <stdio.h> #include <pthread.h> #include <semaphore.h> # using namespace std; u //Store the subrange of computation. struct Range { float up; float down; }; } #define RANGE 10000 //parameters #define THREAD_LIMIT 4 # #define TASKS 10 # float sum = 0; //for storing the result struct Range *compute; //for storing the task sem_t mutex; sem_t space; sem_t content; s void *functionC(void* var) { int i; float SubSum=0; struct Range *SubRange; s while (1) { w //wait for computation assignment sem_wait(&content); SubRange = compute; compute = NULL; sem_post(&space); s if (SubRange == NULL) //termination condition pthread_exit(NULL); p SubSum=0; S //perform the computation for (i=int(SubRange->down*RANGE);i<int(SubRange->up*RANGE);i++) SubSum += ((i+i-1)*1.0/(2*RANGE)+1.0/((i+i-1)*1.0/(2*RANGE) +1))*1.0/RANGE; + //update the total sum / sem_wait(&mutex); sum+=SubSum; //add the output here just to make the display looks better //in principle, we should not place unneccessary stuff within critical
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/01/2010 for the course CS 2342 taught by Professor Dr during the Spring '10 term at HKU.

Page1 / 3

lab6-integral - #include #include #include #include #...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online