Created
January 20, 2020 06:06
-
-
Save rigelrozanski/272d368766e3ab2a468f8b11776c585b to your computer and use it in GitHub Desktop.
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" | |
) | |
func main() { | |
c := make(chan string, 3) | |
var mux1, mux2, mux3 sync.Mutex | |
mux1.Lock() | |
go func() { | |
time.Sleep(2 * time.Second) | |
c <- "first thread" | |
mux1.Unlock() | |
}() | |
mux2.Lock() | |
go func() { | |
time.Sleep(3 * time.Second) | |
c <- "second thread" | |
mux2.Unlock() | |
}() | |
mux3.Lock() | |
go func() { | |
time.Sleep(1 * time.Second) | |
c <- "third thread" | |
mux3.Unlock() | |
}() | |
mux1.Lock() | |
fmt.Println("unlocked 1") | |
mux2.Lock() | |
fmt.Println("unlocked 2") | |
mux3.Lock() | |
fmt.Println("unlocked 3") | |
fmt.Println(<-c) | |
fmt.Println(<-c) | |
fmt.Println(<-c) | |
mux1.Unlock() | |
mux2.Unlock() | |
mux3.Unlock() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment