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
390 B
Go
26 lines
390 B
Go
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) (int, error) {
|
|
w.lock.Lock()
|
|
defer w.lock.Unlock()
|
|
return w.writer.Write(p)
|
|
}
|