[fpc-devel] atomic reads and writes
J. Gareth Moreton
gareth at moreton-family.com
Tue Jul 2 19:00:18 CEST 2019
Really, insofar as x86 is concerned, the only ones that are guaranteed
to be atomic are, I think, the ordinal types, but that still leaves
problems of code such as the following:
if GlobalCounter = 10 then
begin
DoSomething;
Break;
end else
Inc(GlobalCounter);
If two threads are running a loop with the following code at the end, it
could be that GlobalCounter is 9, the condition is checked, but before
Inc(GlobalCounter) is called, the scheduler switches to the other
thread, where GlobalCounter is checked and is still 9, and is then
incremented, and only then does it switch back to the other thread,
where Inc(GlobalCounter) is finally called, thereby setting
GlobalCounter to 11 and breaking the program.
In this instance, a critical section or some other mutex is required, or
the interlocked instructions, all of which incur a performance penalty.
Definitely something for a lot of discussion!
Gareth aka. Kit
On 02/07/2019 08:13, Benito van der Zander wrote:
> Hi,
>
> Am 28.06.19 um 11:24 schrieb George Bakhtadze:
>> If FPC somehow guarantees that a global field (even only of simple
>> types) can be atomically read and written from any thread for ANY
>> platform that is a very strong guarantee and worth mention in
>> documentation.
>
> btw, how would you do that if it is in not guaranteed? especially on arm
>
> Are there functions to do only an atomic reads and writes? (besides
> functions like interlockedincrement that do both, a read and write,
> together)
>
> Best,
> Benito
>
> _______________________________________________
> fpc-devel maillist - fpc-devel at lists.freepascal.org
> https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20190702/2b35af2e/attachment-0001.html>
More information about the fpc-devel
mailing list