简体   繁体   中英

CPU cache friendly byte slice

I have a logger that writes a lot of messages in parallel to stdout. The problem is that messages were written simultaneously and shuffled. So I had to add a mutex and lock before printing:

l.mu.Lock()
fmt.Fprintf(os.Stdout format, v...)
l.mu.Unlock()

I wish to avoid the locking because I need as small latency as possible. But I'm fine with some pauses and I don't care much about order of messages. On my server I have 24 CPUs and each has it's own cache. I have an idea to make per-cpu list of byte slices and then periodically gather all of them and dump to a log. Will this work in practice? I'm feeling that I'm reinventing some existing structure. Could you please recommend an optimal way to do that.

As with many concurrency problems I'd use a concurrent queue to solve this problem.

https://pkg.go.dev/github.com/antigloss/go/concurrent/container/queue seems to be the kind of data structure that you could use to solve your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM