I'm porting a program in C language. It is using two thread to work. * Main thread keeps a status map with a read-write lock. * Update thread keeps reading from a continuous growing file to update the status map. If it read failed, the user can press a button to re-read it.
What's the proper way to do same log in Go? Here is my prototype
var status = struct {
sync.RWMutex
M map[string]string
}
func readFile(filename string) {
// open file
for {
status.Lock()
// update status
status.Unlock()
// run forever util error occurs
}
}
func main() {
go readFile("sample.data")
go readFile("sample2.data")
for {
status.RLock()
fmt.Println(status.M)
status.RUnlock()
// wait for user command (eg. assign new file to read or stop old file or quit)
}
}
I know how to use sync.RWMutex and goroutine, but the problem is how to handle the 'reopen the file', it is on other goroutine and maybe the gorouting have already quit for reading fail, or it may still running, I require to stop the gorouting and restart it.