-
Notifications
You must be signed in to change notification settings - Fork 1
/
pthread.c
69 lines (57 loc) · 1.32 KB
/
pthread.c
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <math.h>
#include <pthread.h>
#define DATASIZE 100 * 1024 * 1024
#define NT 4
long value;
unsigned char *data;
pthread_mutex_t lock;
int tid[NT];
/**
* Compile with gcc -o h4 h4.c -lm -lpthread
*/
int compute_value(int start, int chunk) {
int v = 0;
for(int i = start; i < chunk; i++)
v += (long)cos(sqrt((double)data[i]));
return v;
}
void *run(int *tid)
{
int v = 0;
int chunk = DATASIZE/NT;
if (*tid == 0)
v = compute_value(0, chunk);
else if (*tid == 1)
v = compute_value(chunk, chunk*2);
else if (*tid == 2)
v = compute_value(chunk*2, chunk*3);
else if (*tid == 3)
v = compute_value(chunk*3, DATASIZE);
pthread_mutex_lock(&lock);
value += v;
pthread_mutex_unlock(&lock);
}
int main(int argc, char *argv[])
{
if (argc > 1) srand(atoi(argv[1]));
value = 0;
data = malloc(sizeof(unsigned char) * DATASIZE);
printf("Generating data...\n");
for(int i=0; i < DATASIZE; i++) {
data[i] = (unsigned char)(rand() % 100);
}
pthread_t th[NT];
pthread_mutex_init(&lock, NULL);
printf("Computing...\n");
for (int i = 0; i < NT; i++) {
tid[i] = i;
pthread_create(&th[i], NULL, (void *)run, &tid[i]);
}
for (int i = 0; i < NT; i++)
pthread_join(th[i], NULL);
printf("%ld\n", value);
return 0;
}