efficient / libcuckoo

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

Doubt with the update rule in upsert function #79

Closed k3yavi closed 7 years ago

k3yavi commented 7 years ago

Hi, check the following piece of code from here.

struct TGValue {
std::atomic<uint64_t> count{0};
};

auto upfn = [](TGValue& x) -> void {
    // update the count
    x.count++;
};

TGValue v(1);
<cuckoo_object>.upsert(<key_object>, upfn, v);

My confusion is do I have to define the count variable as atomic type or a normal int would do, if I want to get the correct count for the frequency of the upsert function call. Basically, if the order of the upsert function call doesn't matter to me then does the .upsert function serialize the update/initialization(you can imagine two initialization call concurrently) call to avoid race condition inside the TGValue object? It's not very clear from the documentation.

manugoyal commented 7 years ago

Yes. Access to all data inside the table is serialized, as each item is protected by a mutex. So a normal int should suffice.

k3yavi commented 7 years ago

Thanks @manugoyal !