[SOLVED] what is function declocked(var l: longint) : boolean; ? lazarus / 64-FPC development ---------------------------------------------------------------------- Key-Real · 2024-04-22 07:12 what function declocked(var l: longint) : boolean; procedure inclocked(var l: longint); in the CPU rtl do? ---------------------------------------------------------------------- cdbc · 2024-04-22 09:53 Hi AFAIK, it /tries/ to do an 'InterlockedDecrement', but uses an asm- lock in the process, mind you you, I'm not that strong in asm... You can have a look for yourself, 'TInterfacedObject' uses it in procedure AfterConstruction', from there you can follow it to its asm- code. Regards Benny ---------------------------------------------------------------------- Martin_fr · 2024-04-22 10:14 From the code review (of one of the cpu units / not checked others). It checks if you app is multi threaded, if not it does a normal "decrement by one". If yes, then it does a "locked" decrement. "locked" here means: thread save. If 2 threads do "Somevalue := Somevalue - 1" (or +1 / or other op), then both threads need to read the memory into a register, perform the op, and write it back to mem. The following can happen: - Thread1 read - Thread2 read (the same value, thread 2 has not yet written back) - Thread1 decrement register - Thread1 write back - Thread2 decrement register - Thread2 write back In the last step, Thread2 overwrites the value of Thread1. All together the value will have gone down by only ONE. Even though both threads should have decremented it by 1 and it should be down by TWO. "locked" ops avoid that => but they are slower.