Skip to content

Instantly share code, notes, and snippets.

@armando-couto
Created February 22, 2022 02:23
Show Gist options
  • Save armando-couto/b94f0a3574025ded7ac437937e61f4cb to your computer and use it in GitHub Desktop.
Save armando-couto/b94f0a3574025ded7ac437937e61f4cb to your computer and use it in GitHub Desktop.
Criando um Deadlock em GO.
package main
import (
"fmt"
"sync"
"time"
)
type value struct {
mu sync.Mutex
value int
}
func main() {
var wg sync.WaitGroup
printSum := func(v1, v2 *value) {
defer wg.Done()
v1.mu.Lock()
defer v1.mu.Unlock()
time.Sleep(2 * time.Second)
v2.mu.Lock()
defer v2.mu.Unlock()
fmt.Printf("sum=%v\n", v1.value+v2.value)
}
var a, b value
wg.Add(2)
go printSum(&a, &b)
go printSum(&b, &a)
wg.Wait()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment