[Lazarus] thread safe
Michael Schnell
mschnell at lumino.de
Mon Jun 27 13:23:19 CEST 2011
On 06/27/2011 12:51 PM, Martin wrote:
>
> Why? Where does the definition conflict with the above?
> "A piece of code is *thread-safe* if it can be safely invoked by
> multiple threads at the same time"
It does not *conflict* it is just not useful.
The problem is not "safe invocation" the problem is "*a* piece of code".
An object is invocated with a hidden self pointer. So for the "user"
this "self" is not an "input" as normal parameters are.
By definition, an object is a combination of a data store and a pool of
functions.
Thus (when looking at your definition above) you either can define that
(A) the the "code" is *different* for multiple instances of the object
(even though it only exists once in memory and in the source code), or
you can define or you can define that (B) it's *the same piece of code*
for all instances.
From the point of view of the user both does make sense (if you get him
to be aware of that at all).
Obviously as an example, the (non-thread-safe) TList.add (supposedly)
can happily be used with multiple threads if each thread uses it's own
instance and thus different code (regarding definition (A), while
TThreadList is thread safe (regarding both definitions) and the same
instance can be used by multiple threads.
The reason is, that TList just uses (stack and) class variables (and
decently protected global variables), While with TThreadList
additionally the class variables are decently protected.
OTOH, e.g. TForm is not thread save at all: you can't do multiple
instances and use each of them from it's dedicated thread, because
implicitly TForm uses unprotected global variables.
-Michael
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20110627/31ef0af2/attachment-0003.html>
More information about the Lazarus
mailing list