<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv=Content-Type content="text/html; charset=utf-8"><meta name=Generator content="Microsoft Word 15 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
{font-family:Courier;
panose-1:2 7 4 9 2 2 5 2 4 4;}
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:"Calibri Light";
panose-1:2 15 3 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0cm;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
.MsoChpDefault
{mso-style-type:export-only;
font-family:"Calibri",sans-serif;
mso-fareast-language:EN-US;}
@page WordSection1
{size:612.0pt 792.0pt;
margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]--></head><body lang=SV link="#0563C1" vlink="#954F72" style='word-wrap:break-word'><div class=WordSection1><p class=MsoNormal><span lang=EN-US style='color:#1A1A1A'>It seems </span><span lang=EN-US style='font-family:"Calibri Light",sans-serif;color:#1A1A1A'>ets:select_replace</span><span lang=EN-US style='color:#1A1A1A'> could be your solution.<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US style='color:#1A1A1A'><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-US style='color:#1A1A1A'>The documentation contains this example usage to do a compare-and-swap for a single key:<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US style='font-size:10.0pt;font-family:Courier;color:#1A1A1A'><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-US style='font-size:10.0pt;font-family:Courier;color:#1A1A1A'>[Old] = ets:lookup(T, Key),<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US style='font-size:10.0pt;font-family:Courier;color:#1A1A1A'>New = update_object(Old),<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US style='font-size:10.0pt;font-family:Courier;color:#1A1A1A'>Success = (1 =:= ets:select_replace(T, [{Old, [], [{const, New}]}])),<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US style='font-size:10.0pt;mso-fareast-language:EN-US'><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-US style='mso-fareast-language:EN-US'>/Sverker<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US style='mso-fareast-language:EN-US'><o:p> </o:p></span></p><div style='border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0cm 0cm 0cm'><p class=MsoNormal style='margin-left:65.2pt'><b><span lang=EN-US>From:</span></b><span lang=EN-US> erlang-questions <erlang-questions-bounces@erlang.org> <b>On Behalf Of </b>Frank Muller<br><b>Sent:</b> den 22 april 2021 13:32<br><b>To:</b> Erlang-Questions Questions <erlang-questions@erlang.org><br><b>Subject:</b> ETS: update list value without race condition<o:p></o:p></span></p></div><p class=MsoNormal style='margin-left:65.2pt'><o:p> </o:p></p><div><p class=MsoNormal style='margin-left:65.2pt'><span style='color:#313131;border:none windowtext 1.0pt;padding:0cm'>Hi guys</span><o:p></o:p></p><div style='border:none #313131 1.0pt;padding:0cm 0cm 0cm 0cm;word-spacing:1px'><p class=MsoNormal style='margin-left:65.2pt'><span style='color:#313131'><o:p> </o:p></span></p></div><div style='border:none #313131 1.0pt;padding:0cm 0cm 0cm 0cm;font-size:1rem;word-spacing:1px'><p class=MsoNormal style='margin-left:65.2pt'><span style='color:#313131'>I’ve a little issue I’m unable to solve with the current ETS API.<o:p></o:p></span></p></div><div style='border:none #313131 1.0pt;padding:0cm 0cm 0cm 0cm;font-size:1rem;word-spacing:1px'><p class=MsoNormal style='margin-left:65.2pt'><span style='color:#313131'><o:p> </o:p></span></p></div><div style='border:none #313131 1.0pt;padding:0cm 0cm 0cm 0cm;font-size:1rem;word-spacing:1px'><p class=MsoNormal style='margin-left:65.2pt'><span style='color:#313131'>My table is of type set. Keys are integers and values are list of names:<o:p></o:p></span></p></div><div style='border:none #313131 1.0pt;padding:0cm 0cm 0cm 0cm;font-size:1rem;word-spacing:1px'><p class=MsoNormal style='margin-left:65.2pt'><span style='color:#313131'>123 -> [ jane, john, bob ]<o:p></o:p></span></p></div><div style='border:none #313131 1.0pt;padding:0cm 0cm 0cm 0cm;word-spacing:1px'><p class=MsoNormal style='margin-left:65.2pt'><span style='color:black;border:none windowtext 1.0pt;padding:0cm'>456 -> [ joe, alice ]</span><span style='color:#313131'><o:p></o:p></span></p></div><div style='border:none #313131 1.0pt;padding:0cm 0cm 0cm 0cm;word-spacing:1px'><p class=MsoNormal style='margin-left:65.2pt'><span style='color:black;border:none windowtext 1.0pt;padding:0cm'>…</span><span style='color:#313131'><o:p></o:p></span></p></div><div style='border:none #313131 1.0pt;padding:0cm 0cm 0cm 0cm;word-spacing:1px'><p class=MsoNormal style='margin-left:65.2pt'><span style='color:#313131'><o:p> </o:p></span></p></div><div style='border:none #313131 1.0pt;padding:0cm 0cm 0cm 0cm;word-spacing:1px'><p class=MsoNormal style='margin-left:65.2pt'><span style='color:black;border:none windowtext 1.0pt;padding:0cm'>Process A with Key=123 wants to delete ‘Jane’ while process B with Key=123 wants to add ‘Adam’.</span><span style='color:#313131'><o:p></o:p></span></p></div><div style='border:none #313131 1.0pt;padding:0cm 0cm 0cm 0cm;word-spacing:1px'><p class=MsoNormal style='margin-left:65.2pt'><span style='color:#313131'><o:p> </o:p></span></p></div><div style='border:none #313131 1.0pt;padding:0cm 0cm 0cm 0cm;word-spacing:1px'><p class=MsoNormal style='margin-left:65.2pt'><span style='color:black;border:none windowtext 1.0pt;padding:0cm'>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:#313131'><o:p></o:p></span></p></div><div style='border:none #313131 1.0pt;padding:0cm 0cm 0cm 0cm;word-spacing:1px'><p class=MsoNormal style='margin-left:65.2pt'><span style='color:#313131'><o:p> </o:p></span></p></div><div style='border:none #313131 1.0pt;padding:0cm 0cm 0cm 0cm;word-spacing:1px'><p class=MsoNormal style='margin-left:65.2pt'><span style='color:black;border:none windowtext 1.0pt;padding:0cm'>But this lead to race condition (ex. process B could put back ‘jane’ - last write wins).</span><span style='color:#313131'><o:p></o:p></span></p></div><div style='border:none #313131 1.0pt;padding:0cm 0cm 0cm 0cm;word-spacing:1px'><p class=MsoNormal style='margin-left:65.2pt'><span style='color:#313131'><o:p> </o:p></span></p></div><div style='border:none #313131 1.0pt;padding:0cm 0cm 0cm 0cm;word-spacing:1px'><p class=MsoNormal style='margin-left:65.2pt'><span style='color:black;border:none windowtext 1.0pt;padding:0cm'>Could this problem be solved atomically without involving explicit locking per Key?</span><span style='color:#313131'><o:p></o:p></span></p></div><div style='border:none #313131 1.0pt;padding:0cm 0cm 0cm 0cm;word-spacing:1px'><p class=MsoNormal style='margin-left:65.2pt'><span style='color:#313131'><o:p> </o:p></span></p></div><div style='border:none #313131 1.0pt;padding:0cm 0cm 0cm 0cm;word-spacing:1px'><p class=MsoNormal style='margin-left:65.2pt'><span style='color:black;border:none windowtext 1.0pt;padding:0cm'>Is there any other better way to represent this data set which will not suffer from the race condition?</span><span style='color:#313131'><o:p></o:p></span></p></div></div><div><div style='border:none #313131 1.0pt;padding:0cm 0cm 0cm 0cm;word-spacing:1px'><p class=MsoNormal style='margin-left:65.2pt'><span style='color:#313131'><o:p> </o:p></span></p></div><div style='border:none #313131 1.0pt;padding:0cm 0cm 0cm 0cm;word-spacing:1px'><p class=MsoNormal style='margin-left:65.2pt'><span style='color:#313131'>Thanks<o:p></o:p></span></p></div><div style='border:none #313131 1.0pt;padding:0cm 0cm 0cm 0cm;font-size:1rem;word-spacing:1px'><p class=MsoNormal style='margin-left:65.2pt'><span style='color:#313131'>/Frank<o:p></o:p></span></p></div></div></div></body></html>