Skip to content

Instantly share code, notes, and snippets.

@andyollylarkin
Last active March 20, 2023 19:07
Show Gist options
  • Save andyollylarkin/e9b357c03c1216f74907f02c29665f37 to your computer and use it in GitHub Desktop.
Save andyollylarkin/e9b357c03c1216f74907f02c29665f37 to your computer and use it in GitHub Desktop.
Golang classic mutex with loop implementation
type MyMutex struct {
state int32
}
const (
unlocked = iota
locked
)
func (m *MyMutex) Lock() {
for {
if atomic.CompareAndSwapInt32(&m.state, unlocked, locked) {
return
} else {
continue
}
}
}
func (m *MyMutex) Unlock() {
atomic.StoreInt32(&m.state, unlocked)
}
@andyollylarkin
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment