Re: ETS: update list value without race condition
Jesper Louis Andersen <[email protected]>
| Newsgroups | gmane.comp.lang.erlang.general |
|---|---|
| Message-ID | <CAGrdgiUctV9uRb17Zj9AfxyVuztotmSei7hun_yv0v=5Y1G2Dw@mail.gmail.com> |
Another path is to figure out if the ETS type 'bag' is more suitable in your situation. On Thu, Apr 22, 2021 at 1:46 PM Sverker Eriksson < [email protected]> wrote: > It seems ets:select_replace could be your solution. > > > > The documentation contains this example usage to do a compare-and-swap for > a single key: > > > > [Old] = ets:lookup(T, Key), > > New = update_object(Old), > > Success = (1 =:= ets:select_replace(T, [{Old, [], [{const, New}]}])), > > > > /Sverker > > > > *From:* erlang-questions <[email protected]> *On Behalf > Of *Frank Muller > *Sent:* den 22 april 2021 13:32 > *To:* Erlang-Questions Questions <[email protected]> > *Subject:* ETS: update list value without race condition > > > > Hi guys > > > > I’ve a little issue I’m unable to solve with the current ETS API. > > > > My table is of type set. Keys are integers and values are list of names: > > 123 -> [ jane, john, bob ] > > 456 -> [ joe, alice ] > > … > > > > Process A with Key=123 wants to delete ‘Jane’ while process B with Key=123 > wants to add ‘Adam’. > > > > First, they both needs to read the value associated with Key=123, update > that list accordingly and set back the new value. > > > > But this lead to race condition (ex. process B could put back ‘jane’ - > last write wins). > > > > Could this problem be solved atomically without involving explicit locking > per Key? > > > > Is there any other better way to represent this data set which will not > suffer from the race condition? > > > > Thanks > > /Frank > -- J.