darkautism / lfqueue

Minimize lock-free queue ever!
Do What The F*ck You Want To Public License
131 stars 27 forks source link

Why are you using a node counter in the queue? #1

Closed ruurd closed 7 years ago

ruurd commented 7 years ago

Why are you using a node counter in the queue? The queue is empty if head == tail right? Mutatis mutandis it is not empty if head != tail. So why count if the only thing you're interested in is if the queue is empty or not? (I.e. count == 0 or count != 0)

darkautism commented 7 years ago

This counter is for user who interested the queue size. Because this queue do not have size limit.

If someone use lfq but the consume speed is less then input speed, he will get ENOMEM (Not enough memory). e.g. Someone wanna copy block from SSD to HDD,and he wanna some queue to handle data do something.

ruurd commented 7 years ago

Why would that be of interest to anyone? If you want to know how many enques and deques you do then count them yourself. If you don't count them and want to know how many nodes there are, iterate over them. ¯_(ツ)_/¯ In addition to that, it really is unnecessary to count the number of nodes to find out if the queue isn't empty. And not counting them does away with the atomic increment. Even here less is more.

darkautism commented 7 years ago

You are right. User can count their node itself. I'll remove counter next commit.

darkautism commented 7 years ago

I review the code, and i found some issue if user iterate the inner struct without use pop operate. User souldn't iterate the lfqueue inner struct in multi thread program. Iterate inner struc is not threadsafe in this project. The only secure way is counter (user's atomic counter or inner counter).

I'll add some document for this issue. And use head == tail to check empty queue. But i'm not very sure it is good idea to remove counter.

darkautism commented 7 years ago

ffcc3f581b521f25c782df18a5c516659cf30f18 Remove counter from lfq_clean.