[lug] Fwd: Simple counter?

stimits at comcast.net stimits at comcast.net
Fri May 24 22:48:27 MDT 2013

Could you mmap a file and access as regular memory, then set it up with pthreads and lock/unlock?

----- Original Message -----
From: Bear Giles <bgiles at coyotesong.com>
To: Anthony Foiani <tkil at scrye.com>, Boulder (Colorado) Linux Users Group -- General Mailing List <lug at lug.boulder.co.us>
Sent: Sat, 25 May 2013 02:23:55 -0000 (UTC)
Subject: Re: [lug] Fwd: Simple counter?

It's the concurrency that will get you. Even locks might not work if the underlying filesystem doesn't properly support them. That's why I was thinking along the lines of a named pipe - that way you have a single process on the other end of the pipe.
The database sequence is another possibility. I wouldn't use mysql though - there are lightweight JVM databases (e.g., H2) and you could also investigate SQLite.
Re-reading the same value will always be problematic outside of a database transaction though - how do you know some other process asn't gotten a newer value in the interrim? The only way to be really sure is to cache the information in your own process somehow and again that's adding a lot of weight to the code.

On Fri, May 24, 2013 at 7:09 PM, Anthony Foiani <tkil at scrye.com> wrote:

Derp, I missed these points.

My solution uses a plain text file; a bit slower to parse/write than

binary, but easier to use from the command line.  In particualr:

"Jeffrey S. Haemer" <jeffrey.haemer at gmail.com> writes:

> It would be nice if I could give it a seed -- "start with 12" or

> something -- but that's not critical.

Write the value to the file before you start using it.  Or anytime

after using it.

(But note that I'm only using advisory locks, so if you muck with it

while an instance of 'counter' is trying to use the file, you might

corrupt things.)

> It would be nice if I could ask it what the next number will be

> without bumping the counter.  Also not critical.

It's a text file; just 'cat' it.

> It would even be nice if it offered remote access, so all machines

> could use the same counter.  Again, not critical.

ssh 'cat ...'

Although I could see 'counterd', which is a server... ;->

> Honest: this isn't "How can I write a simple program to do this?"

> It's, "Hasn't someone created a simple, standard utility that does

> this in the past 20 or 30 years?"

It's simple enough that I could write mine in less than an hour (and

it would have been 5min if I weren't being picky about return values).

It's complex enough (look at your requests above -- starting value,

current value, next value, remote access) that there's probably not a

single easy way to satisfy everyone.  E.g., if you were to make it

network transparent, then you'd get complaints from the people who

only want to use it locally.



Web Page:  http://lug.boulder.co.us

Mailing List: http://lists.lug.boulder.co.us/mailman/listinfo/lug

Join us on IRC: irc.hackingsociety.org port=6667 channel=#hackingsociety

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lug.boulder.co.us/pipermail/lug/attachments/20130525/bb9451d1/attachment.html>

More information about the LUG mailing list