RingBuffer: 100% cpu usage
Author: sheldonlyrCreated Jul 18, 2016Updated Aug 29, 2019
$uname -a Linux Lee-Ubuntu 4.4.0-28-generic #47-Ubuntu SMP Fri Jun 24 10:09:13 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux $cat /etc/issue Linux Lee-Ubuntu 4.4.0-28-generic #47-Ubuntu SMP Fri Jun 24 10:09:13 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux $go env Linux Lee-Ubuntu 4.4.0-28-generic #47-Ubuntu SMP Fri Jun 24 10:09:13 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
Test code:
import (
"fmt"
"sync"
"time"
"github.com/Workiva/go-datastructures/queue"
)
var wg sync.WaitGroup
func main() {
wg.Add(2)
q := queue.NewRingBuffer(4096)
go func() { defer wg.Done() for { time.Sleep(3 * time.Second) q.Offer("hello world") } }()
go func() { defer wg.Done() for { str, _ := q.Get() fmt.Println(str) } }()
wg.Wait()
}
Source: Workiva/go-datastructures