Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect m_size update #7

Open
jy5275 opened this issue Mar 12, 2022 · 0 comments
Open

Incorrect m_size update #7

jy5275 opened this issue Mar 12, 2022 · 0 comments

Comments

@jy5275
Copy link

jy5275 commented Mar 12, 2022

Any thread reach https://github.com/tstarling/thread-safe-lru/blob/master/thread-safe-lru/lru-cache.h#L378 is an error, since that branch doesn't evict a node, and leads to m_size not following the actual size of the list. But in current code this error might happen. If the cache state is as follow (with three inserting threads):

m_maxSize = 3
msize = 3
Thread 1 execution - https://github.com/tstarling/thread-safe-lru/blob/master/thread-safe-lru/lru-cache.h#L301 (insert)
Thread 2 execution - https://github.com/tstarling/thread-safe-lru/blob/master/thread-safe-lru/lru-cache.h#L301 (insert)
Thread 3 execution - https://github.com/tstarling/thread-safe-lru/blob/master/thread-safe-lru/lru-cache.h#L301 (insert)
List is empty

This state is possible, if the cache already has 3 items, then each of the 3 inserting threads find the exceeding size, then evicts one node, but yet has't insert its own node into list.

At this time, if another thread comes and tries to evict, it will find the list is empty then go to https://github.com/tstarling/thread-safe-lru/blob/master/thread-safe-lru/lru-cache.h#L378.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant