On Monday, 22 February 2021 at 08:00:06 UTC, Ali Çehreli wrote:
I achieve it with 'flock' (man 2 flock) on Linux. My case is
different thoug: It allows me to have single writer and many
readers of program data, which is kept inside a cache
directory. All instances start with read permissionss to
exclude any potential writer. Once a writer grabs the lock for
writing, no other writer or reader can exist, etc. A nice side
effect that I realized was since there is only one writer, the
lock file itself stores progress information so that other
instances can say something like "Waiting for the writer to
finish: 75%" by reading from the lock file itself. :)
Works like a charm except flock operations are not atomic; so
some care is needed. For example, the writer cannot assume
there were no other writers when it "drops" its access right to
a reader. It has to check some signature to make sure that
everything is expected.
Implementing progress reporting in that way is cool. :)
I'll looking into using flock to implement this functionality
when I port this program to FreeBSD/Linux (I'll probably try
implementing this functionality in another application first as I
only infrequently use those operating systems as desktop
environments).