cameron314 / concurrentqueue

A fast multi-producer, multi-consumer lock-free concurrent queue for C++11
Other
9.87k stars 1.69k forks source link

How about a size_max() function? #282

Open oviano opened 2 years ago

oviano commented 2 years ago

We currently have size_approx() and I understand how that works.

However, I am finding that when I use this queue I frequently want to run some clean up code at the end where I dequeue all the items I added and free them up or do something to them or whatever.

I don't think I can rely on size_approx() as I think it would be plausible that the value wouldn't be guaranteed to be correct if my clean-up code (in thread A) were to run too soon after the last item was added to the queue (in thread B)? I suppose it's unlikely, and I admit I'm not really too sure.

Well I'm finding that to be sure, I have an atomic counter which I increment before I call enqueue(), and decrease after I call dequeue(). You could call this a conservative version of size_approx(), or size_max().

I know then that I can loop until this value is zero, calling try_dequeue() and then I'm definitely going to catch all the items I added.

Is this something that might be added? Or.....as I alluded to above, maybe it's unnecessary?