Forum > Beginners
Record Locking
(1/1)
Andy Anderson:
I've read all the references I
could find but couldn't find an
example to put it all together.
Can someone supply a link to an
example of non OOP record/region
locking?
Thanks in advance.
Andy Anderson:
Well, I guess there is no way in Free Pascal
to lock/unlock records/regions for Windows
OS's.
My workaround will be to use the LOCKFILEEX/UNLOCKFILEEX API's.
Laksen:
It depends on what you need.
If you need to lock a shared variable you can use critical sections, mutexes, semaphores, or events.
Critical sections are probably by far the simplest to understand. You can take a look at the syncobjs unit to see some easy to use object oriented classes for this.
Edit: Ah, if you need non-OOP code you can also take a look at that unit. It uses the standard platform independent interface in the RTL to do the different things.
For example, a critical section:
--- Code: ---var cs: TRTLCriticalSection;
// Call this to initialize
InitCriticalSection(cs);
// And this when you are done with it
DoneCriticalSection(cs);
// And to lock some resource
EnterCriticalSection(cs);
... do stuff with whatever needs locking
LeaveCriticalSection(cs);
--- End code ---
The CS variable would be a single lock that only prevents others from entering the same. So you will want one for each thing that you are locking.
Andy Anderson:
Thanks, Laksen. I'll take a look at the critical sections material. Curiously, the Windows API's I mentioned require a type specification almost identical to the Unix based FLock type for the FpFcntl call.
Navigation
[0] Message Index