Nel linguaggio di programmazione Go, è bytes.Buffer
thread-safe? AFAIK, its documentation non menziona la sicurezza del thread.Il file go bytes.buffer è sicuro?
risposta
No.
La documentazione Go segue una semplice regola: se non è detto esplicitamente che l'accesso simultaneo a qualcosa è sicuro, non lo è.
Utilizzare la chiamata di funzione io.Pipe() che fornisce una coppia di oggetti connessi (* PipeReader, * PipeWriter) per lettura/scrittura sincrona. Questo potrebbe essere fatto in parallelo, ed è un thread sicuro.
No, ma si può facilmente avvolgere in una struttura thread-safe!
per le cose semplici:
type Buffer struct {
b bytes.Buffer
m sync.Mutex
}
func (b *Buffer) Read(p []byte) (n int, err error) {
b.m.Lock()
defer b.m.Unlock()
return b.b.Read(p)
}
func (b *Buffer) Write(p []byte) (n int, err error) {
b.m.Lock()
defer b.m.Unlock()
return b.b.Write(p)
}
func (b *Buffer) String() string {
b.m.Lock()
defer b.m.Unlock()
return b.b.String()
}
..e usarlo come al solito var buf Buffer
, ecc
Vuoi saperne di più di bytes.Buffer? Sentiti libero di scegliere la ciliegina:
func (b *Buffer) Bytes() []byte {
b.m.Lock()
defer b.m.Unlock()
return b.b.Bytes()
}
func (b *Buffer) Cap() int {
b.m.Lock()
defer b.m.Unlock()
return b.b.Cap()
}
func (b *Buffer) Grow(n int) {
b.m.Lock()
defer b.m.Unlock()
b.b.Grow(n)
}
func (b *Buffer) Len() int {
b.m.Lock()
defer b.m.Unlock()
return b.b.Len()
}
func (b *Buffer) Next(n int) []byte {
b.m.Lock()
defer b.m.Unlock()
return b.b.Next(n)
}
func (b *Buffer) ReadByte() (c byte, err error) {
b.m.Lock()
defer b.m.Unlock()
return b.b.ReadByte()
}
func (b *Buffer) ReadBytes(delim byte) (line []byte, err error) {
b.m.Lock()
defer b.m.Unlock()
return b.b.ReadBytes(delim)
}
func (b *Buffer) ReadFrom(r io.Reader) (n int64, err error) {
b.m.Lock()
defer b.m.Unlock()
return b.b.ReadFrom(r)
}
func (b *Buffer) ReadRune() (r rune, size int, err error) {
b.m.Lock()
defer b.m.Unlock()
return b.b.ReadRune()
}
func (b *Buffer) ReadString(delim byte) (line string, err error) {
b.m.Lock()
defer b.m.Unlock()
return b.b.ReadString(delim)
}
func (b *Buffer) Reset() {
b.m.Lock()
defer b.m.Unlock()
b.b.Reset()
}
func (b *Buffer) Truncate(n int) {
b.m.Lock()
defer b.m.Unlock()
b.b.Truncate(n)
}
func (b *Buffer) UnreadByte() error {
b.m.Lock()
defer b.m.Unlock()
return b.b.UnreadByte()
}
func (b *Buffer) UnreadRune() error {
b.m.Lock()
defer b.m.Unlock()
return b.b.UnreadRune()
}
func (b *Buffer) WriteByte(c byte) error {
b.m.Lock()
defer b.m.Unlock()
return b.b.WriteByte(c)
}
func (b *Buffer) WriteRune(r rune) (n int, err error) {
b.m.Lock()
defer b.m.Unlock()
return b.b.WriteRune(r)
}
func (b *Buffer) WriteString(s string) (n int, err error) {
b.m.Lock()
defer b.m.Unlock()
return b.b.WriteString(s)
}
func (b *Buffer) WriteTo(w io.Writer) (n int64, err error) {
b.m.Lock()
defer b.m.Unlock()
return b.b.WriteTo(w)
}
Dovrebbe usare rwmux – xiaoyi
- 1. È sicuro eliminare manualmente il file tempdb.mdf?
- 2. Ottenere "bytes.Buffer non implementa io.Writer" messaggio di errore
- 3. Write Operation Cost [Go lang]
- 4. Il thread NSPersistentStoreCoordinator è sicuro?
- 5. Go: è sicuro usare il pacchetto gob per salvare i dati in un file per un uso successivo?
- 6. È sicuro rinominare il file delle migrazioni di Django?
- 7. È sicuro il thread SecurityContextHolder?
- 8. Il thread SecureRandom è sicuro?
- 9. Il thread closesocket è sicuro?
- 10. Il thread libuv è sicuro?
- 11. È sicuro il thread unique_ptr?
- 12. Il thread java.util.Hashtable è sicuro?
- 13. jstack - file noto non è sicuro
- 14. È un file char statico finale [] sicuro?
- 15. Il canale bufferizzato di Go è lockless?
- 16. Python rimuove il file in modo sicuro
- 17. È aperto(). Read() sicuro?
- 18. È sicuro ignorare Carthage.resolved?
- 19. Come testare la funzione Go contenente log.Fatal()
- 20. Filetto NSUserDefault è sicuro?
- 21. Ottieni file inode in Go
- 22. Il thread WCF ClientBase è sicuro?
- 23. Il thread di console.writeline è sicuro?
- 24. È sicuro il thread Java Regex?
- 25. È sicuro sovrascrivere `release` per il debug?
- 26. Il mio tipo di codice è sicuro?
- 27. Il thread di Windows Win32 è sicuro?
- 28. Il cucchiaio non è sicuro in Haskell?
- 29. Il thread Netty Channel.write è sicuro?
- 30. Il thread java.util.Calendar è sicuro o no?
Solo curioso, è quella regola documentata da qualche parte? – Ztyx