On Tue, Dec 3, 2019 at 6:31 PM Liam <networkimp...@gmail.com> wrote: > > Erm... not seeing how cas works to try a read-lock, which admits multiple > callers...
Something like this should work: TryLock: if CompareAndSwap(&v,0,1) { door.Lock() return true } return false Unlock: door.Unlock() Store(&v,0) > > On Tuesday, December 3, 2019 at 5:12:08 PM UTC-8, Robert Engels wrote: >> >> Can’t code on the phone but a cas on the atomic then take the lock. But if >> you never need to block you don’t even need the lock. >> >> On Dec 3, 2019, at 6:52 PM, Liam Breck <networ...@gmail.com> wrote: >> >> >> Which looks like...? >> >> On Tue, Dec 3, 2019, 4:50 PM Robert Engels <ren...@ix.netcom.com> wrote: >>> >>> I would use an atomic and a lock instead of two locks. >>> >>> > On Dec 3, 2019, at 6:35 PM, burak serdar <bse...@computer.org> wrote: >>> > >>> > On Tue, Dec 3, 2019 at 5:21 PM Liam Breck <networ...@gmail.com> wrote: >>> >> >>> >> I have a problem that is trivially solved via >>> >> >>> >> door sync.RWMutex >>> >> >>> >> func Reader() T { >>> >> if !door.TryRLock() { // missing in stdlib :-( >>> >> return busy >>> >> } >>> >> defer door.RUnlock() >>> >> ... >>> >> } >>> >> >>> >> func Writer() { >>> >> door.Lock() >>> >> defer door.Unlock() >>> >> ... >>> >> } >>> >> >>> >> How does one achieve this in Go? >>> > >>> > Two locks and a bool? >>> > >>> > var door=sync.Mutex{} >>> > var x=sync.Mutex{} >>> > var b bool >>> > >>> > func trylock() bool { >>> > x.Lock() >>> > if b { >>> > x.Unlock() >>> > return false >>> > } >>> > b=true >>> > door.Lock() >>> > x.Unlock() >>> > return true >>> > } >>> > >>> > unlock: >>> > >>> > x.Lock() >>> > b=false >>> > door.Unlock() >>> > x.Unlock() >>> > >>> > >>> > >>> > >>> >> >>> >> -- >>> >> You received this message because you are subscribed to the Google >>> >> Groups "golang-nuts" group. >>> >> To unsubscribe from this group and stop receiving emails from it, send >>> >> an email to golan...@googlegroups.com. >>> >> To view this discussion on the web visit >>> >> https://groups.google.com/d/msgid/golang-nuts/CAKvHMgTO%3DxfFQ_u7aO9UE-1vHHEKmdhr47sro2mnp6DkEb6mPA%40mail.gmail.com. >>> > >>> > -- >>> > You received this message because you are subscribed to the Google Groups >>> > "golang-nuts" group. >>> > To unsubscribe from this group and stop receiving emails from it, send an >>> > email to golan...@googlegroups.com. >>> > To view this discussion on the web visit >>> > https://groups.google.com/d/msgid/golang-nuts/CAMV2RqrDeBNhkeswg%2BhdCf1kSzMEJduota%3D6UrNq4z2PQRtzEQ%40mail.gmail.com. >>> > -- > You received this message because you are subscribed to the Google Groups > "golang-nuts" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to golang-nuts+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/golang-nuts/2d284bfc-3916-425a-9dd1-59b216ef23e2%40googlegroups.com. -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAMV2RqqsFo1wxsP_P9iacNNP2DR%2BPut4TwcfKtcun4Oy9CJbEA%40mail.gmail.com.