On 13/08/14 16:24, Alex Balashov wrote:
On 08/13/2014 10:21 AM, AliReza Khoshgoftar Monfared wrote:
Another concern is that, if I specify a number of child processes in my config (e.g. children=4), then will it be safe to increment the hash table entries in the route block upon receipt of messages? i.e will the table entries that reside in the shared memory be precise? or do I need to lock/unlock them while updating?
htable operations are thread-safe, as implemented under the hood. You do not explicitly need to lock anything.
While that is true for htable (direct) operations, one should be careful when using hash table items in expressions.
So:
- next is an atomic increment of $sht(a=>x)
$var(x) = $shtinc(a=>x);
- but next might rise races:
$sht(a=>x) = $sht(a=>x) + 1;
because first the value is read, an add expression is evaluated and then the item in hash table is set to new value. During the evaluation of the expression, the current process can lose CPU, and another process can update the item. So you can make it safer with:
sht_lock("a=>x"); $sht(a=>x) = $sht(a=>x) + 1; sht_unlock("a=>x");
Cheers, Daniel