Last active
December 10, 2019 13:28
-
-
Save podanypepa/f97f426fdfbac2f1068f0a58e22ab285 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" | |
"time" | |
) | |
type Sem struct { | |
c chan struct{} | |
name string | |
locker string | |
} | |
func Semaphore(name string) Sem { | |
return Sem{ | |
make(chan struct{}, 1), | |
name, | |
"", | |
} | |
} | |
func (s *Sem) Lock(l string) { | |
s.c <- struct{}{} | |
s.locker = l | |
fmt.Printf("lock %s locked by %s\n", s.name, l) | |
} | |
func (s *Sem) Unlock() { | |
<-s.c | |
fmt.Printf("lock %s unlocked by %s\n", s.name, s.locker) | |
} | |
func main() { | |
sum := 0 | |
a := Semaphore("A") | |
go func() { | |
a.Lock("1") | |
defer a.Unlock() | |
fmt.Println("1. go") | |
sum += 1 | |
}() | |
go func() { | |
a.Lock("2") | |
defer a.Unlock() | |
fmt.Println("2. go") | |
sum += 1 | |
}() | |
time.Sleep(2 * time.Second) | |
fmt.Println(sum) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment