Restricting D applications to a single instance

Ali Çehreli acehreli at yahoo.com
Mon Feb 22 08:00:06 UTC 2021


On 2/21/21 7:28 PM, Preetpal wrote:

 > I decided to implement what I meant (single application (executable)
 > instance restriction) using the Windows API (since my application only
 > runs on Windows currently anyways):

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.

Ali



More information about the Digitalmars-d-learn mailing list