Advertisement
siritinga

Productor-consumidor sencillo

May 28th, 2013
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.62 KB | None | 0 0
  1. // Productor-consumidor sencillo.
  2. // Comunidad de Go en espaƱol: https://plus.google.com/u/0/communities/105964860738231827761
  3.  
  4. package main
  5.  
  6. import "fmt"
  7. import "time"
  8.  
  9. type Task struct {
  10.     num      int
  11.     priority int
  12.     index    int
  13. }
  14.  
  15. func main() {
  16.     channel := make(chan int)
  17.  
  18.     go prod(channel)
  19.     go cons(channel)
  20.  
  21.     time.Sleep(1 * time.Minute)
  22. }
  23.  
  24. func prod(tocons chan int) {
  25.     for i := 0; ; i++ {
  26.         tocons <- i
  27.         fmt.Println("Prod: task ", i)
  28.     }
  29. }
  30.  
  31. func cons(fromprod chan int) {
  32.     for {
  33.         task := <-fromprod
  34.         time.Sleep(1000 * time.Millisecond)
  35.         fmt.Println("                Cons: task ", task)
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement