Created
February 22, 2022 02:23
-
-
Save armando-couto/b94f0a3574025ded7ac437937e61f4cb to your computer and use it in GitHub Desktop.
Criando um Deadlock em GO.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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