Last active
March 20, 2023 19:07
-
-
Save andyollylarkin/e9b357c03c1216f74907f02c29665f37 to your computer and use it in GitHub Desktop.
Golang classic mutex with loop implementation
This file contains hidden or 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
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) | |
} |
Author
andyollylarkin
commented
Feb 19, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment