You are not logged in.
Pages: 1
Hi,
so far, it was safe to do following:
LockedList.Safe.Lock;
try
for ItemIdx := 0 to LockedList.Count - 1 do
ItemClear(TMypSession(LockedList[ItemIdx]));
LockedList.Clear;
finally
LockedList.Safe.UnLock;
end;
with the recent change, having seen that in many places, it will dead lock at the clear method.
Further, having methods like Add/Remove/... automatically thread safe, and others like Delete not being thread safe is, imho, not a good idea, as one ALWAYS has to look at the documentation, to be sure what's happining. And as the documemantion does not work within the IDE, that means jumping around in the sources.
THe old SafeAdd/Safe... methods where more self-identifying and certainly easy to use.
Please, roll back that change. :-)
Offline
LockedList.Clear would never make a race condition.
It calls the same LockedList.Safe lock, which is reentrant.
If you have a race condition, it doesn't comes from this change.
Offline
So Safe.Lock allows me to "lock" multiple times from within the same thread without deadlocking? It only blocks other threads at the same time? Didn't know/test that before. Would be cool.
Still leaves, imo, the big problem, that some methods do locking automatically (like Add/Remove/Clear), while others like Delete do not, which is not obvious, when using the list.
Offline
@sakura
Yes, this is how TRTLCriticalSection works: they are re-entrant by definition.
Delete() from an index can not be thread-safe, because it uses an index which may change if something is added in a background thread. An explicit lock is needed.
Remove() make a search + delete so it can be made thread-safe.
Offline
I particularly like the pattern that returns the locked instance. Then, you can always block and release the necessary access to the list.
It is very simple to implement as you do not need to override the methods in the original list.
with MyList.Locked do //Locked make the lock and returns the FList itself
try
Add(..);
Delete(..)
Clear;
finally
MyList.unlock;
end;
But the implementation of mORMot is also very good.
Last edited by macfly (2020-03-09 12:50:55)
Offline
Is there an option to allow multiple read single write logic?
Like TMultiReadExclusiveWriteSynchronizer do.
Useful when object must be thread safe with big amount of read operations and rare write operations.
Last edited by George (2020-07-07 15:51:14)
Offline
Is there an option to allow multiple read single write logic?
Like TMultiReadExclusiveWriteSynchronizer do.
Useful when object must be thread safe with big amount of read operations and rare write operations.
I usually use TPasMPMultipleReaderSingleWriterLock from PasMP which works with both Delphi and FPC (Windows and Linux).
Last edited by trx (2020-07-08 08:11:32)
Offline
Pages: 1