chanx icon indicating copy to clipboard operation
chanx copied to clipboard

Why is Len() not accurate ?

Open bruceauyeung opened this issue 1 year ago • 3 comments

// Len returns len of In plus len of Out plus len of buffer.
// It is not accurate and only for your evaluating approximate number of elements in this chan,
func (c *UnboundedChan[T]) Len() int {
	return len(c.In) + c.BufLen() + len(c.Out)
}

And if i wanna check whether this chan is empty or not, how should i do ?

bruceauyeung avatar Nov 15 '24 09:11 bruceauyeung

because the dim of the three values is not atomic

smallnest avatar Nov 16 '24 01:11 smallnest

because the dim of the three values is not atomic

Alright then. But at last, if chan is consumed out and no more values pushed in, Len() will be 0, right?

bruceauyeung avatar Nov 18 '24 08:11 bruceauyeung

because the dim of the three values is not atomic

Alright then. But at last, if chan is consumed out and no more values pushed in, Len() will be 0, right?

yes

smallnest avatar Nov 18 '24 09:11 smallnest