summaryrefslogtreecommitdiffstats
path: root/04_exercise/main.c
blob: 8a57abbe0de671eda8d1887b0678e7c28a29752b (plain) (blame)
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
#include "threadpool.h"

#include <stdio.h>
#include <stdlib.h>

static TASK(long, fib, long)

    long fib(long n) {
    if (n <= 1)
        return n;
    // fprintf(stderr, "Thread %ld: fib being called with %d \n", pthread_self(), n);
    fib_fut *a = fibAsync((fib_fut[]){fibFuture(n - 1)});
    fib_fut *b = fibAsync((fib_fut[]){fibFuture(n - 2)});

    return fibAwait(a) + fibAwait(b);
}
// Show this runs with only one thread and one place in the work queue
int main() {
    setvbuf(stdout, NULL, _IONBF, 0);
    if (tpInit(4) != 0)
        perror("Thread Pool initialization failed"), exit(-1);
    atexit(&tpRelease);
    for (long i = 25; i <= 50; ++i)
        printf("fib(%2li) = %li\n", i, fib(i));

    return 0;
}