efficient / libcuckoo

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

Questions with 'erasing' elements when iterating #32

Closed DJuego closed 7 years ago

DJuego commented 8 years ago

Hi! Thank you very much! for this Concurrent Container (open-source and free). It has very good references. The only 'competitor' that I know is Junction...

I am a newbie. I have a doubt :-?

I have two threads. In thread 1 the map is iterated and the element are shown/printed (read only). I am using lock_table().

In thread 2 the map is iterated. A condition is evaluated for each element. If the condition is true, the element must be removed.

Well How can i do this? :-(

If i use the lock_table() in thread 2, there are a deadlock.


//THREAD 1

auto lt = m_manager.m_mapadeconexiones.lock_table();
for (const auto& it : lt)
{
   SHOW(it->second);
}

//THREAD 2

auto lt = m_mapadeconexiones.lock_table();

auto it = lt.begin();
while (it != lt.end())
{

if (CONDITION_FOR_REMOVING (it->second)))
{
    m_mapadeconexiones.erase(it->first); //<------ DEADLOCK!!!
}
else
{
++it;
}
}

is it a mistaken approach to the problem/solution? Thanks for the help! A sample will be incredible. :-}

DJuego

manugoyal commented 8 years ago

Hi @DJuego,

The reason you're getting a deadlock is that whenever you create a locked table, the object takes all the locks on the table, so no operations on the original table object will work. Currently the locked table only supports spawning iterators, but we could certainly add the functionality to do all the normal table operations (like find, insert, erase, etc) on the locked table.

For now, unfortunately, there's no good work around for this that I can think of, since unlocking and re-locking the table to erase an element would be pretty inefficient. I'll try to add the functionality you're looking for soon.

DJuego commented 8 years ago

Thanks for your swift answer!

you're getting a deadlock is that whenever you create a locked table, the object takes all the locks on the table, so no operations on the original table object will work.

Yes. I suspected it. :-) However; i was unable to find the erase operation in the locked table. I was pretty confused. That's why I believed that there was an alternative approach... Your answer comforts me somehow, :-)

I'll try to add the functionality you're looking for soon.

Thanks! I will be awaiting eagerly. :-P You can be sure! :-)

DJuego

DJuego commented 7 years ago

I only wanted to go on record that my intense interest in this feature survives 💯 :-P Happy New Year and thanks for the upgrades! 🥇

DJuego

manugoyal commented 7 years ago

This should be doable on future-master now.