Last active
October 1, 2019 15:12
-
-
Save phuctm97/f217fc951cf9d1d7d8cd8b53f1cfe689 to your computer and use it in GitHub Desktop.
Readers-Writers Problem with Go
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
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