[REBOL] Antwort: How does one persist data? Re:(2)
From: kracik:mbox:dkm:cz at: 12-Oct-2000 18:59
Hi,
I forgot to include some description how it should work. The guarded
file is not %lockdir/lock, but any other file - because read-only
accesses to the file don't need to be guarded, and would fail if the
guarded file is currently renamed. You should make a directory
%lockdir/ with an empty file %lock
then you should use:
obtain-semaphore
something: read %my-guarded-file.txt
write %my-guarded-file.txt "something new"
release-semaphore
If you want to only read from your file, you can use just
something: read %my-guarded-file.txt
print something
If I remember correctly, you must only guard one situation,
read-modify-write
, because two simultaneous reads do not interfere,
and two simultaneous writes are a logic error of your program.
obtain-semaphore function uses loop and random wait interval to have a
better chance of succeeding if several processes run it simultaneously.
release-semaphore function should be simpler, loop is not necessary:
release-semaphore: func [] [
if not error? try [ rename %lockdir/lock.locked %lock ] [
return
]
print "Could not release semaphore"
quit
]
--
Michal Kracik
[Sharriff--Aina--med-iq--de] wrote: