[erlang-questions] Preventing a race condition when using dets/ets

Sverker Eriksson <>
Wed May 7 17:44:57 CEST 2008


Paul Fisher wrote:
> On Wed, 2008-05-07 at 08:48 -0500, Dave Bryson wrote:
>   
>> Say for example I have the following code:
>>
>> {_,N} = ets:lookup(?MODULE,counter),
>> ok = ets:insert(?MODULE, {counter, N+1}).
>>     
>
> Two options:
> 1) use ets:update_counter/3, if it is really an integer value with the
> table being public
> 2) have a gen_server process "own" the ets table and perform all
> operations on behalf of callers (gen_server:call/2, 3) with the table
> being privat
Just thought I would mention...
As from R12B-2, one call to ets:update_counter/3 can update several 
integer-counters within the same ets-object.

ets:insert(Tab,{key,Done,LeftToDo}).
:
ets:update_counter(Tab,key,[{2,1},{3,-1}]).

/Sverker, Erlang/OTP Ericsson



More information about the erlang-questions mailing list