Skip to content

Instantly share code, notes, and snippets.

@phuctm97
Last active October 1, 2019 15:12
Show Gist options
  • Save phuctm97/f217fc951cf9d1d7d8cd8b53f1cfe689 to your computer and use it in GitHub Desktop.
Save phuctm97/f217fc951cf9d1d7d8cd8b53f1cfe689 to your computer and use it in GitHub Desktop.
Readers-Writers Problem with Go
package readerswriters
import "sync"
var mutex = new(sync.RWMutex)
func writer(data []byte) {
mutex.Lock()
defer mutex.Unlock()
// Write to data.
}
func reader(data []byte) {
mutex.RLock()
defer mutex.RUnlock()
// Read from data.
}
func main() {
for i := 0; i < NumberOfReaders; i++ {
go reader()
}
for i := 0; i < NumberOfWriters; i++ {
go writer()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment