[Lazarus] Weird object variables access - fpc issue?

Sven Barth pascaldragon at googlemail.com
Tue Oct 24 11:03:36 CEST 2017


Am 24.10.2017 10:49 schrieb "el es via Lazarus" <
lazarus at lists.lazarus-ide.org>:

On 24/10/17 09:11, Mattias Gaertner via Lazarus wrote:
> On Tue, 24 Oct 2017 08:59:36 +0100
> el es via Lazarus <lazarus at lists.lazarus-ide.org> wrote:
>
>> [...]
>> begin
>>   repeat
>>   until not ThreadNowInUse;  //
>>
>>   try
>>     ThreadNowInUse :=true;
>
> This is not thread safe.
>
> Mattias
>

Because separated by 'try' ?

I see; Will it be enough to put it like

try
  repeat
  until not ThreadNowInUse;
  ThreadNowInUse := true;

...

?


You should look at InterlockedCompareExchange(), everything else is an
accident waiting to happen (except for a critical section, but that would
be overkill on the other hand).

Regards,
Sven


I haven't ran into problems with my routine yet,
but I wasn't really calling it in a very 'tight loop'

(as it is used for timing/'profiling' the calls to hardware 'master node'
routines
 and for deciding whether the thread should go to sleep; so there is always
ample time
 between calls to ThreadNow() and it's only called in context of the
thread; so the
 'poor mans semaphore' isn't really that required - it's just a safeguard)

(probably if I need timing measurements, every other type of thread I
will ever define, will have their own ThreadNow call)

I realize I could just use 'naked' GetSystemTime + SystemTimeToDateTime
pair with a local variable;
or define the above pair as a nested function in the thread, that'll
serialize it enough...

Anyone ever thought of a thread-safe global Now() call ?

el es

--
_______________________________________________
Lazarus mailing list
Lazarus at lists.lazarus-ide.org
https://lists.lazarus-ide.org/listinfo/lazarus
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20171024/ec2701de/attachment.html>


More information about the Lazarus mailing list