gods icon indicating copy to clipboard operation
gods copied to clipboard

circularbuffer values is not concurrent safe

Open tsingsun opened this issue 2 years ago • 1 comments

when circularbuffer Values use in concurrent case, one queue enqueue. the other dequeue per 100ms,and in dequeue get all values to handler.

// Values returns all elements in the queue (FIFO order).
func (queue *Queue) Values() []interface{} {
	values := make([]interface{}, queue.Size(), queue.Size())
	for i := 0; i < queue.Size(); i++ {
		if i >= len(values) || ((queue.start+i)%queue.maxSize) >= len(queue.values) {
			fmt.Println("i:", i, "len(values):", len(values),"size",queue.Size(), "queue.start+i:", queue.start+i, "queue.maxSize:", queue.maxSize,"len(queue.values):", len(queue.values),queue.String())
		}
		values[i] = queue.values[(queue.start+i)%queue.maxSize]
	}
	return values
}

// output //i: 91 len(values): 91 size 92 queue.start+i: 91 queue.maxSize: 100 len(queue.values): 100 CircularBuffer

tsingsun avatar Jun 08 '22 08:06 tsingsun

@tsingsun GoDS is by design not implemented to be thread-safe, hence you need to protect the reads/writes with their respective mutexes in Golang.

emirpasicspread avatar Jun 08 '22 14:06 emirpasicspread