Skip to content

Instantly share code, notes, and snippets.

@prembhaskal
Created March 14, 2022 17:07
Show Gist options
  • Save prembhaskal/54554ab385bcec53ab28ba0707767a8b to your computer and use it in GitHub Desktop.
Save prembhaskal/54554ab385bcec53ab28ba0707767a8b to your computer and use it in GitHub Desktop.
package parallel
import "sync"
type clientsync struct {
val int
}
func (c *clientsync) Add(x int) int {
return c.val + x
}
type librarySync struct {
c *clientsync
mx sync.Mutex
}
func NewLibrarySync() *librarySync {
c := &clientsync{
val: 0,
}
return &librarySync{
c: c,
}
}
func (l *librarySync) C() *clientsync {
l.mx.Lock()
defer l.mx.Unlock()
return l.c
}
func (l *librarySync) UpdateClient() {
l.mx.Lock()
defer l.mx.Unlock()
newVal := l.c.val + 1
l.c = &clientsync{newVal}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment