<div dir="auto">Indeed it’s Sverker.</div><div dir="auto"><br></div><div dir="auto">This CAS example you provided doesn’t involve any full table scan, right?</div><div><br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;padding-left:1ex;border-left-color:rgb(204,204,204)"><div lang="SV" link="#0563C1" vlink="#954F72" style="word-wrap:break-word"><div class="m_3920001818650942815WordSection1"><p class="MsoNormal"><span lang="EN-US" style="color:rgb(26,26,26)"><br>It seems </span><span lang="EN-US" style="font-family:"Calibri Light",sans-serif;color:rgb(26,26,26)">ets:select_replace</span><span lang="EN-US" style="color:rgb(26,26,26)"> could be your solution.<u></u><u></u></span></p><p class="MsoNormal"><span lang="EN-US" style="color:rgb(26,26,26)"><u></u> <u></u></span></p><p class="MsoNormal"><span lang="EN-US" style="color:rgb(26,26,26)">The documentation contains this example usage to do a compare-and-swap for a single key:<u></u><u></u></span></p><p class="MsoNormal"><span lang="EN-US" style="font-size:10pt;font-family:Courier;color:rgb(26,26,26)"><u style="font-family:Courier"></u> <u style="font-family:Courier"></u></span></p><p class="MsoNormal"><span lang="EN-US" style="font-size:10pt;font-family:Courier;color:rgb(26,26,26)">[Old] = ets:lookup(T, Key),<u style="font-family:Courier"></u><u style="font-family:Courier"></u></span></p><p class="MsoNormal"><span lang="EN-US" style="font-size:10pt;font-family:Courier;color:rgb(26,26,26)">New = update_object(Old),<u style="font-family:Courier"></u><u style="font-family:Courier"></u></span></p><p class="MsoNormal"><span lang="EN-US" style="font-size:10pt;font-family:Courier;color:rgb(26,26,26)">Success = (1 =:= ets:select_replace(T, [{Old, [], [{const, New}]}])),</span></p></div></div><div lang="SV" link="#0563C1" vlink="#954F72" style="word-wrap:break-word"><div class="m_3920001818650942815WordSection1"><p class="MsoNormal"><span lang="EN-US" style="font-size:10pt;font-family:Courier;color:rgb(26,26,26)"><u style="font-family:Courier"></u><u style="font-family:Courier"></u></span></p><p class="MsoNormal"><span lang="EN-US" style="font-size:10pt"><u></u> <u></u></span></p><p class="MsoNormal"><span lang="EN-US">/Sverker<u></u><u></u></span></p><p class="MsoNormal"><span lang="EN-US"><u></u> <u></u></span></p><div style="border-style:solid none none;border-top-width:1pt;padding:3pt 0cm 0cm;border-top-color:rgb(225,225,225)"><p class="MsoNormal" style="margin-left:65.2pt"><b><span lang="EN-US">From:</span></b><span lang="EN-US"> erlang-questions <<a href="mailto:erlang-questions-bounces@erlang.org" target="_blank">erlang-questions-bounces@erlang.org</a>> <b>On Behalf Of </b>Frank Muller<br><b>Sent:</b> den 22 april 2021 13:32<br><b>To:</b> Erlang-Questions Questions <<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a>><br><b>Subject:</b> ETS: update list value without race condition<u></u><u></u></span></p></div><p class="MsoNormal" style="margin-left:65.2pt"><u></u> <u></u></p><div><p class="MsoNormal" style="margin-left:65.2pt"><span style="border:1pt none windowtext;padding:0cm;color:rgb(49,49,49)">Hi guys</span><u></u><u></u></p><div style="border:1pt none rgb(49,49,49);padding:0cm;word-spacing:1px"><p class="MsoNormal" style="margin-left:65.2pt"><span style="color:rgb(49,49,49)"><u></u> <u></u></span></p></div><div style="border:1pt none rgb(49,49,49);padding:0cm;font-size:1rem;word-spacing:1px"><p class="MsoNormal" style="margin-left:65.2pt"><span style="color:rgb(49,49,49)">I’ve a little issue I’m unable to solve with the current ETS API.<u></u><u></u></span></p></div><div style="border:1pt none rgb(49,49,49);padding:0cm;font-size:1rem;word-spacing:1px"><p class="MsoNormal" style="margin-left:65.2pt"><span style="color:rgb(49,49,49)"><u></u> <u></u></span></p></div><div style="border:1pt none rgb(49,49,49);padding:0cm;font-size:1rem;word-spacing:1px"><p class="MsoNormal" style="margin-left:65.2pt"><span style="color:rgb(49,49,49)">My table is of type set. Keys are integers and values are list of names:<u></u><u></u></span></p></div><div style="border:1pt none rgb(49,49,49);padding:0cm;font-size:1rem;word-spacing:1px"><p class="MsoNormal" style="margin-left:65.2pt"><span style="color:rgb(49,49,49)">123 -> [ jane, john, bob ]<u></u><u></u></span></p></div><div style="border:1pt none rgb(49,49,49);padding:0cm;word-spacing:1px"><p class="MsoNormal" style="margin-left:65.2pt"><span style="border:1pt none windowtext;padding:0cm;color:black">456 -> [ joe, alice ]</span><span style="color:rgb(49,49,49)"><u></u><u></u></span></p></div><div style="border:1pt none rgb(49,49,49);padding:0cm;word-spacing:1px"><p class="MsoNormal" style="margin-left:65.2pt"><span style="border:1pt none windowtext;padding:0cm;color:black">…</span><span style="color:rgb(49,49,49)"><u></u><u></u></span></p></div><div style="border:1pt none rgb(49,49,49);padding:0cm;word-spacing:1px"><p class="MsoNormal" style="margin-left:65.2pt"><span style="color:rgb(49,49,49)"><u></u> <u></u></span></p></div><div style="border:1pt none rgb(49,49,49);padding:0cm;word-spacing:1px"><p class="MsoNormal" style="margin-left:65.2pt"><span style="border:1pt none windowtext;padding:0cm;color:black">Process A with Key=123 wants to delete ‘Jane’ while process B with Key=123 wants to add ‘Adam’.</span><span style="color:rgb(49,49,49)"><u></u><u></u></span></p></div><div style="border:1pt none rgb(49,49,49);padding:0cm;word-spacing:1px"><p class="MsoNormal" style="margin-left:65.2pt"><span style="color:rgb(49,49,49)"><u></u> <u></u></span></p></div><div style="border:1pt none rgb(49,49,49);padding:0cm;word-spacing:1px"><p class="MsoNormal" style="margin-left:65.2pt"><span style="border:1pt none windowtext;padding:0cm;color:black">First, they both needs to read the value associated with Key=123, update that list accordingly and set back the new value.</span><span style="color:rgb(49,49,49)"><u></u><u></u></span></p></div><div style="border:1pt none rgb(49,49,49);padding:0cm;word-spacing:1px"><p class="MsoNormal" style="margin-left:65.2pt"><span style="color:rgb(49,49,49)"><u></u> <u></u></span></p></div><div style="border:1pt none rgb(49,49,49);padding:0cm;word-spacing:1px"><p class="MsoNormal" style="margin-left:65.2pt"><span style="border:1pt none windowtext;padding:0cm;color:black">But this lead to race condition (ex. process B could put back ‘jane’ - last write wins).</span><span style="color:rgb(49,49,49)"><u></u><u></u></span></p></div><div style="border:1pt none rgb(49,49,49);padding:0cm;word-spacing:1px"><p class="MsoNormal" style="margin-left:65.2pt"><span style="color:rgb(49,49,49)"><u></u> <u></u></span></p></div><div style="border:1pt none rgb(49,49,49);padding:0cm;word-spacing:1px"><p class="MsoNormal" style="margin-left:65.2pt"><span style="border:1pt none windowtext;padding:0cm;color:black">Could this problem be solved atomically without involving explicit locking per Key?</span><span style="color:rgb(49,49,49)"><u></u><u></u></span></p></div><div style="border:1pt none rgb(49,49,49);padding:0cm;word-spacing:1px"><p class="MsoNormal" style="margin-left:65.2pt"><span style="color:rgb(49,49,49)"><u></u> <u></u></span></p></div><div style="border:1pt none rgb(49,49,49);padding:0cm;word-spacing:1px"><p class="MsoNormal" style="margin-left:65.2pt"><span style="border:1pt none windowtext;padding:0cm;color:black">Is there any other better way to represent this data set which will not suffer from the race condition?</span><span style="color:rgb(49,49,49)"><u></u><u></u></span></p></div></div><div><div style="border:1pt none rgb(49,49,49);padding:0cm;word-spacing:1px"><p class="MsoNormal" style="margin-left:65.2pt"><span style="color:rgb(49,49,49)"><u></u> <u></u></span></p></div><div style="border:1pt none rgb(49,49,49);padding:0cm;word-spacing:1px"><p class="MsoNormal" style="margin-left:65.2pt"><span style="color:rgb(49,49,49)">Thanks<u></u><u></u></span></p></div><div style="border:1pt none rgb(49,49,49);padding:0cm;font-size:1rem;word-spacing:1px"><p class="MsoNormal" style="margin-left:65.2pt"><span style="color:rgb(49,49,49)">/Frank<u></u><u></u></span></p></div></div></div></div></blockquote></div></div>