Skip to content

Instantly share code, notes, and snippets.

@rigelrozanski
Created January 20, 2020 06:06
Show Gist options
  • Save rigelrozanski/272d368766e3ab2a468f8b11776c585b to your computer and use it in GitHub Desktop.
Save rigelrozanski/272d368766e3ab2a468f8b11776c585b to your computer and use it in GitHub Desktop.
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