[fpc-pascal]two questions plz help
Stefan Ziegenbalg
stefan.ziegenbalg at mailbox.tu-dresden.de
Wed Jan 16 12:57:30 CET 2002
Jonas Maebe wrote:
> This isn't MT-safe at all. If two threads are in the "while lock_mem do
> delay(1);" loop, it's possible that when something else sets it to
> false, they both check it before the other one has set it to true. You
> need to do the testing and setting as an atomic operation.
But the probability of that event is very low. It can been avoid by
setting lock_mem to a thread specific ID (for example the PID):
var lock_mem : longint;
procedure getmem2(var pt:pointer; size:longint);
var pid:longint;
begin
pid:=getpid; { maybe getpid needs more time than delay(1) }
repeat
while lock_mem<>0 do
delay(1);
lock_mem:=pid;
delay(1);
until lock_mem=pid;
getmem(pt,size);
lock_mem:=0;
end;
I think this should avoid any pathological cases. :-)
Regards
Stefan
----------------------------------------------
mailto:stefan.ziegenbalg at mailbox.tu-dresden.de
http://www.sziegenbalg.de
http://www.simage.org
------------------------------------------------------------------
In a world without walls and fences, who needs Windows and Gates ?
More information about the fpc-pascal
mailing list