You ask specific answers to a very general question.
Basically, in addition to user interface operations, you should protect every access to shared memory / resources to avoid two potentially competing threads:
- read inconsistent memory
- write memory at the same time
- try to use the same resource simultaneously with multiple threads ... until the resource is thread safe.
As a rule, I believe that any other workflow is safe, including operations that provide access to non-shared memory or non-shared objects.
For example, consider this object:
type TThrdExample = class private FValue: Integer; public procedure Inc; procedure Dec; function Value: Integer; procedure ThreadInc; procedure ThreadDec; function ThreadValue: Integer; end; ThreadVar ThreadValue: Integer;
Inc, Dec, and Value are methods that work on the FValue field. These methods are not thread safe until you protect them with some kind of synchronization mechanism. It could be MultipleReaderExclusiveWriterSinchronizer for the Value function and CriticalSection for Inc and Dec.
The ThreadInc and ThreadDec methods work on the ThreadValue variable, which is defined as ThreadVar, so I consider ThreadSafe because the memory they access is not shared between threads ... each call from another thread will access a different memory address.
If you know that by design a class should be used only in one thread or inside other synchronization mechanisms, you can consider this thread safe by design.
If you want more specific answers, I suggest you try a more specific question.
Sincerely.
EDIT: Perhaps someone will say that integer fields are a bad example, because you can consider whole atom operations on Intel / Windows, so there is no need to protect it ... but I hope you get the idea.
jachguate
source share