Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<pthread.h>
#define SIZE 10000
#define NUM_THREADS 5
int result;
pthread_mutex_t lock;
struct args
{
int *arr;
int start;
int end;
};
void * accumulate(void * in)
{
//TODO implement accumlate
return NULL;
}
int main ()
{
result=0;
int arr[SIZE];
for(int i=0;i<SIZE;i++)
arr[i]=1;
pthread_t threads[NUM_THREADS-1];
struct args thread_args[NUM_THREADS-1];
// TODO create a team of thread, each thread must take SIZE/NUM_THREADS to accumulate
// TODO use struct args to pass arguments to the accumulate function
// TODO main thread must participate in the calculation
// TODO make sure all threads finised
printf("sum is %d\n", result);
}