Thread-safe operation with Stdout and Stderr

I described the problem here:

tell me which thread-safe buffer should I use?

type lockedReader struct {
    mu sync.Mutex
    r io.Reader
}

func (r *lockedReader) Read(b []byte) (n int, err error) {
    r.mu.Lock()
    defer r.mu.Unlock()
    return r.r.Read(b)
}

type lockedWriter struct {
    mu sync.Mutex
    w io.Writer
}

func (w *lockedWriter) Write(b []byte) (n int, err error) {
    w.mu.Lock()
    defer w.mu.Unlock()
    return w.w.Write(b)
}

func main() {
    stdout := &lockedWriter{w: os.Stdout}
    stdin := &lockedReader{r: os.Stdin}
    // use stdout and stdin instead of os.Stdout and os.Stdin.
}
1 Like

thank you very much

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.