fib.go 936 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. //go:build ignore
  5. // +build ignore
  6. // Compute Fibonacci numbers with two goroutines
  7. // that pass integers back and forth. No actual
  8. // concurrency, just threads and synchronization
  9. // and foreign code on multiple pthreads.
  10. package main
  11. import (
  12. big "."
  13. "runtime"
  14. )
  15. func fibber(c chan *big.Int, out chan string, n int64) {
  16. // Keep the fibbers in dedicated operating system
  17. // threads, so that this program tests coordination
  18. // between pthreads and not just goroutines.
  19. runtime.LockOSThread()
  20. i := big.NewInt(n)
  21. if n == 0 {
  22. c <- i
  23. }
  24. for {
  25. j := <-c
  26. out <- j.String()
  27. i.Add(i, j)
  28. c <- i
  29. }
  30. }
  31. func main() {
  32. c := make(chan *big.Int)
  33. out := make(chan string)
  34. go fibber(c, out, 0)
  35. go fibber(c, out, 1)
  36. for i := 0; i < 200; i++ {
  37. println(<-out)
  38. }
  39. }