Skip to content

Instantly share code, notes, and snippets.

@guilhermebruzzi
Last active December 16, 2015 08:39
Show Gist options
  • Save guilhermebruzzi/5407741 to your computer and use it in GitHub Desktop.
Save guilhermebruzzi/5407741 to your computer and use it in GitHub Desktop.
Vários elevadores consumindo pessoas
// Copyright 2013 Francisco Souza. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"time"
"flag"
)
type Barbeiro struct {
dormindo int
capacidade int
count int
clienteNaCadeira Cliente
clientesEsperando []Cliente
}
type Cliente struct {
Name string
}
func cortaCabelo(barbeiroStruct *Barbeiro){
time.Sleep(10)
barbeiroStruct.count--;
fmt.Printf("Cliente %q cortou o cabelo.\n", barbeiroStruct.clienteNaCadeira.Name)
lenEsperando := len(barbeiroStruct.clientesEsperando)
if lenEsperando > 0 {
barbeiroStruct.clienteNaCadeira = barbeiroStruct.clientesEsperando[0]
barbeiroStruct.clientesEsperando = barbeiroStruct.clientesEsperando[1:lenEsperando]
} else{
barbeiroStruct.clienteNaCadeira = Cliente{Name: "ninguem"}
barbeiroStruct.dormindo = 1
}
}
func barbeiroProc(capacidade int) chan<- Cliente {
barbeiroStruct := new(Barbeiro)
barbeiroStruct.capacidade = capacidade
barbeiroStruct.count = 0
barbeiroStruct.dormindo = 1
barbeiroStruct.clienteNaCadeira = Cliente{Name: "ninguem"}
barbeiroStruct.clientesEsperando = make([]Cliente, capacidade)
barbeiroStruct.clientesEsperando = barbeiroStruct.clientesEsperando[capacidade:capacidade]
clientes := make(chan Cliente)
go func() {
for cliente := range clientes {
if barbeiroStruct.count <= barbeiroStruct.capacidade {
barbeiroStruct.count++
if barbeiroStruct.count == 1 {
barbeiroStruct.dormindo = 0
barbeiroStruct.clienteNaCadeira = cliente
} else{
barbeiroStruct.clientesEsperando = append(barbeiroStruct.clientesEsperando, cliente)
}
go cortaCabelo(barbeiroStruct)
} else{
fmt.Printf("Cliente %q foi embora\n", cliente.Name)
}
fmt.Printf("Cadeira %q: Espera: %v\n", barbeiroStruct.clienteNaCadeira.Name, barbeiroStruct.clientesEsperando)
}
}()
return clientes
}
func main() {
var nChairs, nClientes int
flag.IntVar(&nChairs, "chairs", 2, "Number of chairs")
flag.IntVar(&nClientes, "clients", 10, "Number of clients")
flag.Parse()
clientes := []Cliente{
{Name: "Bob"}, {Name: "Mary"}, {Name: "Thomas"},
{Name: "John"}, {Name: "Peter"}, {Name: "Ken"},
{Name: "Patricia"}, {Name: "Ane"}, {Name: "Alice"},
}
ch := barbeiroProc(nChairs)
for i := 0; i < nClientes; i++ {
ch <- clientes[i % len(clientes)]
}
time.Sleep(time.Second)
close(ch)
}
// Copyright 2013 Francisco Souza. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"time"
"strconv"
)
type Person struct {
Name string
}
func elevator(people chan Person, i int) {
go func() {
for p := range people {
fmt.Printf(strconv.Itoa(i) + ": Transporting %q...\n", p.Name)
time.Sleep(1e9)
}
}()
}
func main() {
people := []Person{
{Name: "Bob"}, {Name: "Mary"}, {Name: "Thomas"},
{Name: "John"}, {Name: "Peter"}, {Name: "Ken"},
{Name: "Patricia"}, {Name: "Ane"}, {Name: "Alice"},
}
canal_person := make(chan Person)
for i:=0; i < 4; i++ {
elevator(canal_person, i)
}
count := 0
for _, person := range people {
canal_person <- person
count++
}
close(canal_person)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment