xant / libhl

Simple and fast C library implementing a thread-safe API to manage hash-tables, linked lists, lock-free ring buffers and queues
GNU Lesser General Public License v3.0
420 stars 117 forks source link

ht_foreach_value return remove, will lose item #34

Closed cxm1997 closed 1 month ago

cxm1997 commented 3 months ago

ht_create(65536, 65536, NULL);

TEST_1 remove all item: add 100000 item to hashtable, before ht_foreach_value, ht_count() is 100000 then ht_foreach_value. in callback function return HT_ITERATOR_REMOVE, record entry callback function count is 51261 after, ht_count() is 48739

TEST_2 contine all item: add 100000 item to hashtable, before ht_foreach_value, ht_count() is 100000 then ht_foreach_value, in callback function return HT_ITERATOR_CONTINUE, record entry callback function count is 100000 after, ht_count() is 100000

actually, I want to traverse all items and delete them so, when ht_foreach_value callback return HT_ITERATOR_REMOVE, will lose item.

cxm1997 commented 3 months ago

use master code to test

cxm1997 commented 3 months ago

test code at https://gist.github.com/cxm1997/06f99190be5e553f5b476286f333ad14

run result: S0 0 S1 100000 S2 100000 S3 48739 continue 100000 remove 51261

xant commented 1 month ago

Thanks! The fix is in 1ab7aec75fdb3e65e11aefbd6ea017e7dcdcd8bf I also added a test which was reproducing the issue before fixing the code in hashtable.c