attractivechaos / klib

A standalone and lightweight C library
http://attractivechaos.github.io/klib/
MIT License
4.22k stars 559 forks source link

lock question #57

Open geemang2000 opened 9 years ago

geemang2000 commented 9 years ago

I plan to use khash as a cache in a long lived server process (web server).

At times I'll need to synchronize add / updates to khash.
I'd like to avoid locking the entire table. I wanted to confirm this strategy.

cache_entry; kh_put(..) lock() if (put_ret == 0) get & free entry add / replace entry unlock()

lh3 commented 9 years ago

You need to put kh_put() in the critical section – i.e. you need to put lock above kh_put(). If locking is frequent, khash is not the best for you. There are lock-free hash table implementations.

geemang2000 commented 9 years ago

Do you know of an actively supported "C" lock-free hash table impl? I've googled and googled... seems you're in league of your own with a "live" project.

ldcanh commented 9 years ago

Hello geemang200 and lh3,

I have a problem same to you to use khash with multi-thread (multicore also). It works very well with single thread, but with multithread when i use pthread mutex to lock/unlock with kh_put for add/del emlement, the performance is not good. I found some lock-free hashtable implement C: http://www.liblfds.org/, https://github.com/urcu/userspace-rcu and https://github.com/LPD-EPFL/ASCYLIB but it seems complicated and difficult to use. Can you give me some advices?

Thank you,

lh3 commented 9 years ago

I am not aware of good lock-free hash table implementation. I guess to be lock-free, the table size has to be fixed because malloc() needs to be locked anyway.