Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: Atomic update counter in a file

by RichardK (Parson)
on Nov 18, 2015 at 15:41 UTC ( #1148031=note: print w/replies, xml ) Need Help??

in reply to Atomic update counter in a file

You didn't say which operating system you're on, and the implementation details differ. But in general file systems just aren't atomic so these things are difficult, and they never work on network file systems.

However, you might find it easier to split this into 2 files, one just for the locking that you never need to remove so there's no race, and the other one for the data.

# pseudo code :) lock(.counter.lock); update(.counter); unlock(.counter.lock); # of course you need to read the counter under the lock too lock(.counter.lock); read(.counter); unlock(.counter.lock);

As long as everyone who accesses the counter uses the lock you should be ok

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1148031]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (4)
As of 2020-05-30 12:41 GMT
Find Nodes?
    Voting Booth?
    If programming languages were movie genres, Perl would be:

    Results (171 votes). Check out past polls.