]> Cypherpunks.ru repositories - gostls13.git/blob - misc/cgo/gmp/fib.go
f453fcf1843b7c13123398a212c0d8cc64935817
[gostls13.git] / misc / cgo / gmp / fib.go
1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 //go:build ignore
6 // +build ignore
7
8 // Compute Fibonacci numbers with two goroutines
9 // that pass integers back and forth.  No actual
10 // concurrency, just threads and synchronization
11 // and foreign code on multiple pthreads.
12
13 package main
14
15 import (
16         big "."
17         "runtime"
18 )
19
20 func fibber(c chan *big.Int, out chan string, n int64) {
21         // Keep the fibbers in dedicated operating system
22         // threads, so that this program tests coordination
23         // between pthreads and not just goroutines.
24         runtime.LockOSThread()
25
26         i := big.NewInt(n)
27         if n == 0 {
28                 c <- i
29         }
30         for {
31                 j := <-c
32                 out <- j.String()
33                 i.Add(i, j)
34                 c <- i
35         }
36 }
37
38 func main() {
39         c := make(chan *big.Int)
40         out := make(chan string)
41         go fibber(c, out, 0)
42         go fibber(c, out, 1)
43         for i := 0; i < 200; i++ {
44                 println(<-out)
45         }
46 }