forked from SeanBarber/homework7
-
Notifications
You must be signed in to change notification settings - Fork 0
/
macro_threads.c
42 lines (36 loc) · 872 Bytes
/
macro_threads.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
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#define NUMTASKS 25000
#define NANOSECONDS_PER_SECOND 1E9
#define FIBNUM 26
int fib(int x) {
if (x == 0)
return 0;
else if (x == 1)
return 1;
return fib(x - 1) + fib(x - 2);
}
void * workerthread(void * tid){
fib(FIBNUM);
return NULL;
}
int main(){
pthread_t thr[NUMTASKS];
struct timespec before, after;
printf("running fib(%d) %d times\n",FIBNUM,NUMTASKS);
clock_gettime(CLOCK_REALTIME, &before);
for (int i = 0; i < NUMTASKS; i++) {
int rcode;
if ((rcode = pthread_create(&thr[i], NULL, workerthread, NULL))) {
fprintf(stderr, "Error in pthread_create: %d\n", rcode);
exit(EXIT_FAILURE);
}
}
for (int i = 0; i < NUMTASKS; i++) {
pthread_join(thr[i], NULL);
}
clock_gettime(CLOCK_REALTIME, &after);
return 0;
}