Skip to content

Commit 83001df

Browse files
committed
2018/10/12: Gopherpalooza SF talk slides and HTML
1 parent 1a2190b commit 83001df

22 files changed

+3618
-0
lines changed

2018/10/12/gopherpalooza/fib.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
func fib(n int) int {
2+
if n < 2 {
3+
return n
4+
}
5+
6+
ch := make(chan int)
7+
go fibGo(n-1, ch)
8+
go fibGo(n-2, ch)
9+
10+
x := <-ch
11+
y := <-ch
12+
return x + y
13+
}
14+
15+
func fibGo(n int, ch chan int) {
16+
ch <- fib(n)
17+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
int fib(int n) {
2+
if (n < 2)
3+
return n;
4+
5+
int x = spawn fib(n-1);
6+
int y = spawn fib(n-2);
7+
8+
sync;
9+
10+
return x + y;
11+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <pthread.h>
4+
5+
void *th_fib(void *arg);
6+
7+
int fib(int n) {
8+
if (n < 2)
9+
return n;
10+
11+
pthread_t t1, t2;
12+
int n_1 = n-1, n_2 = n-2;
13+
pthread_create(&t1, NULL, th_fib, &n_1);
14+
pthread_create(&t2, NULL, th_fib, &n_2);
15+
16+
int *x, *y;
17+
pthread_join(t1, (void **)&x);
18+
pthread_join(t2, (void **)&y);
19+
20+
int result = *x + *y;
21+
free(x);
22+
free(y);
23+
return result;
24+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
void *th_fib(void *arg) {
2+
int *n = (int *)arg;
3+
int *result = malloc(sizeof(int));
4+
*result = fib(*n);
5+
return (void *)result;
6+
}

0 commit comments

Comments
 (0)