Best approach for expanding collections

40 views
Skip to first unread message

Luka Havrlišan

unread,
Apr 5, 2025, 6:38:48 PMApr 5
to Spring4D
I plan to migrate my code from Delphi's generic collections to Spring4D's. The approach I used was having a wrapper class that exposed the same methods that TList<> has, and each method call was wrapped with a lock. I'll probably be doing the same thing for Spring's IList<>, so I guess my question is, is that the best approach for thread-safe lists (thread-safe in the context of freely using the lists from anywhere in a read-only fashion, although the list holder can alter the list)?

The second question concerns the additional (helper) methods for specific IList<T> types. The most common scenario would be selecting some objects from the list with the Where method. What would be the best approach for such methods? Declaring global methods and passing them to the Where function, adding a wrapper (which would fit the locking scenario mentioned earlier but loses the ability to create collections via TCollections), or something else?

I'm still in the process of getting to know the spring4d code, so don't mind if I'm overseeing something obvious! 

Lastly, thanks for working on this awesome library. I've already done some minor tests in comparison to my current lists, and let's just say I've been missing out on both optimizations and versatility your library offers.

Stefan Glienke

unread,
Apr 5, 2025, 6:51:01 PMApr 5
to Spring4D
I suggest against putting locks into the list but surround any code that works with a list in the containing class.
The reason is that not only the list itself but also other code that uses such a list usually needs the lock - just one example: you could think of adding a lock into the Add method, which would protect two concurrently called Add methods - but what about a loop over the list? You would need a similar approach like TThreadList (terrible name, by the way) in the RTL with its LockList/UnlockList methods.

Freely using lists from anywhere in a read-only fashion in a thread-safe way can only be achieved if the list is immutable - meaning you guarantee that nobody changes their content.

If you need specific collection types for transferring items in a concurrent environment you might want to have a look at the collection types from OTL because they are designed specifically for that scenario.
Reply all
Reply to author
Forward
0 new messages