[erlang-questions] compare and set semantics using ets?

Anton Lebedevich mabrek@REDACTED
Mon Jan 14 13:08:58 CET 2013


Is it possible to implement atomic compare-and-set semantics using ets?

Currently it allows to increment counter and return new value atomically
but "don't touch the counter if someone touched it before me" is quite
tricky or impossible to achieve.

I might be digging in a wrong direction, the problem is following:
There is a shared limited resource (i.e. memory). Incoming requests
require some (specified in headers) amount of the resource which is
freed on completion. Each request is served via it's own process. The
process needs to allocate the resource and serve the request or refuse
if there is not enough resource available.

Simple (and slow on high request rate) solution is to implement
gen_server that handles allocation requests and either grants them or
refuses based on calculated total.

https://github.com/ferd/dispcount uses ets counter with known initial
value to implement compare-and-set but in my case the initial value
(currently allocated amount) is unknown.

Anton Lebedevich.

More information about the erlang-questions mailing list