-
Notifications
You must be signed in to change notification settings - Fork 0
/
contention.c
60 lines (51 loc) · 1.42 KB
/
contention.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
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
#include "ntime.h"
#include "utils.h"
#define N 200
#define LOOP_NUM 20000000
int thread_timings[N];
pthread_barrier_t barrier;
void loopFunc() {
int x = 0;
int i;
for (i = 0; i < LOOP_NUM; i++) {
x++;
}
return;
}
void* threadLoopFunc(void* args) {
pthread_barrier_wait(&barrier);
uint64_t st = get_time_in_nsecs();
loopFunc();
uint64_t ed = get_time_in_nsecs();
int el = (int) ((ed-st) / (uint64_t) 1000000);
*(int *)args = el;
return NULL;
}
int main() {
int j;
for (j = 0; j < 5; j++) loopFunc();
uint64_t st = get_time_in_nsecs();
loopFunc();
uint64_t ed = get_time_in_nsecs();
int el = (int)(ed-st) / 1000000;
printf("Took %d msecs with only 1 thread running\n", el);
pthread_barrier_init(&barrier, N);
pthread_t threads[N];
int i;
for (i = 0; i < N; i++) {
pthread_create(&threads[i], NULL, threadLoopFunc, &thread_timings[i]);
}
for (i = 0; i < N; i++) {
pthread_join(threads[i], NULL);
}
int total_msecs = 0;
for (i = 0; i < N; i++) {
total_msecs += thread_timings[i];
}
printf("Took %d msecs with %d threads", total_msecs / N, N);
return 0;
}