🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #2424

Re: read/write semiphores?

#2424
From: Remy Lebeau (TeamB)
Date: Fri, 02 Mar 2007 03:09
32 lines
2305 bytes
"Robert G. Hoover" <not.a.real.address@anywhere.sorry> wrote in
message news:45e797e2$1@newsgroups.borland.com...

> What's the best way to guard against one program writing
> to a file when another program is reading the same file?

Open the file with writing access denied to other processes.

LoadFromFile() opens the file with writing access denied.  If another
program already has the file open for writing, LoadFromFile() will
fail with an exception thrown.  If another program tries to open the
file for writing after LoadFromFile() has it open, the other program
will fail.

SaveToFile() creates the file with exclusive access.  If another
program already has the file open for any reason, SaveToFile() will
fail.  If another program tries to open the file for any reason after
SaveToFile() has opened the file, the other program will fail.

> I need to make sure that no additions are lost because it
> happens at just the wrong time after my first app reads the
> file but before my first app saves the revised file.

Then you should re-think your design.  Having both programs accessing
the same file at the same time is not a good choice.  They need to
communicate with each other so that they can coordinate their access.
Otherwise, you should offload the file access to another dedicated
process that they share so that they do not access the file directly.


Gambit
Technical Details
Message-ID: <45e79d5c@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <45e797e2$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,425c3313c9d5dcd8
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <45e797e2$1@newsgroups.borland.com>
Subject: Re: read/write semiphores?
Date: Thu, 1 Mar 2007 19:09:29 -0800
Lines: 34
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1409
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409
NNTP-Posting-Host: 75.83.34.171
Message-ID: <45e79d5c@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1172807004 75.83.34.171 (1 Mar 2007 19:43:24 -0800)
Path: arc1-ca.pugleaf.net!g2news2.google.com!news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail