efficient / libcuckoo

A high-performance, concurrent hash table
Other
1.61k stars 275 forks source link

Further clarifications #57

Closed necromancersecret closed 7 years ago

necromancersecret commented 7 years ago

Hello I am thinking of using libcuckoohash map in my c++ applicaiton. Seeking few advice before i get fully plunged into it. I am designing some real time streaming application where i am hashmapping few data depending upon the unique keys. So its like from my main thread, i am creating the libcuckoo hashmap like

typedef cuckoohash_map<unsigned int, myObjectStruct > cuckooTable;
cuckooTable *cuckooTableHashMap = NULL;
cuckooTableHashMap = new cuckooTable;

and then sharing cuckooTableHashMap instance with all the 24 threads, now each 24 threads at any second are streaming the data , and i am also inserting the data using the unique key after parsing the streaming data. so lets consider a thread T1() -> parses the streamed data and extracts the unique key and then tries inserting the object on that extracted key using this code like below

if (!pMAP->find(uID, tempObject)){
  if (pMAP->insert(imei, tempObject1)){
  prtinf("Inserted");   
}
}
else
pMAP->update(uID, tempObject)

now also exactly at same time T4() thread, is also doing the same thing, in that thread it parses the data and found the same key and try inserting using the same above code only. in my poc , i have not considered of taking care of any lock and etc. Should I ? Is that approach is fine ?

Johnson Neo

necromancersecret commented 7 years ago

????

rob-p commented 7 years ago

Haven't you already asked this question (and received an answer from @manugoyal) last year? I don't think anything about libcuckoo has changed in this respect in the intervening time.

necromancersecret commented 7 years ago

Dear rob

This time question about locking :) and is my approach fine, ?

manugoyal commented 7 years ago

Hey Johnson,

You shouldn't have to do any explicit locking yourself, but I think there could be a race condition in your approach to doing the insert. Essentially, the element could be inserted in between the call to find and insert by another thread, which would make your insert fail.

I think what you might want to look into is the upsert function. It essentially combines the functionality of your find-insert-update logic into one threadsafe routine. So if you replace your entire loop with a line like follows:

pMAP->upsert(uID, [&tempObject](mapped_type& val) { val = tempObject; }, tempObject);

I think that should do what you want.