You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
26 lines
396 B
Go
26 lines
396 B
Go
2 years ago
|
package synchelper
|
||
|
|
||
|
import (
|
||
|
"io"
|
||
|
"sync"
|
||
|
)
|
||
|
|
||
|
// NewSyncWriter 返回写互斥的 writer
|
||
|
func NewSyncWriter(writer io.Writer) io.Writer {
|
||
|
return &syncWriter{
|
||
|
writer: writer,
|
||
|
lock: sync.Mutex{},
|
||
|
}
|
||
|
}
|
||
|
|
||
|
type syncWriter struct {
|
||
|
writer io.Writer
|
||
|
lock sync.Mutex
|
||
|
}
|
||
|
|
||
|
func (w *syncWriter) Write(p []byte) (n int, err error) {
|
||
|
w.lock.Lock()
|
||
|
defer w.lock.Unlock()
|
||
|
return w.writer.Write(p)
|
||
|
}
|