I have a stringlist myStringList
with abouth 100 values and I'm doing asynchronous access to it.I would like to know if it is thread safe to do this :
currentIndex := myStringList.IndexOf(wantedValue);
or I always have to do this :
criticalS.Enter;
try
currentIndex := myStringList.IndexOf(wantedValue);
finally
criticalS.Leave;
end;
currentIndex
may be invalid by the time the thread has executed thecriticalS.Leave
operations. What could it ever be used for? Instead, try to define whole operations on your shared data, and protect those whole operations with locking. A public interface like that ofTStrings
is completely useless in a concurrent scenario. Of course, if you don't have concurrent modifications the first is completely sufficient. – Engrave