[Lazarus] thread safe

Mattias Gaertner nc-gaertnma at netcologne.de
Fri Jun 10 16:59:02 CEST 2011


On Fri, 10 Jun 2011 16:17:15 +0200
Michael Schnell <mschnell at lumino.de> wrote:

> On 06/10/2011 04:01 PM, Reimar Grabowski wrote:
> > > The "piece of code" can be a function that is called with a parameter. 
> > > It can be thread safe if each thread calls it with a different parameter 
> > > value (say a number denoting the thread) and not thread safe if some 
> > > threads call it with the same parameter.  
> > These kind of functions are called not thread safe.
> That is exactly what I wanted to say.

No.
Whether a function is "thread safe" does not depend on how you call it.
If a function can fail if it is called with wrong parameters or by the
wrong Thread, then it is as Reimar wrote: it is not thread safe.

And now follows a lot of text, how to safely call not thread safe
functions, which might have been useful on a forum for parallel
programming, if it would not invent misleading terms like "completely
thread safe":

> (With Objects this is more obscure 
> than with the ANSI C code example, as the instance parameter is hidden 
> and the application programmer usually is not aware if same.)
> 
> Regarding objects this definition does not make much senses, as this 
> would make make most objects non thread safe, while they would be 
> thread-safe if each thread uses it's dedicated instance.
> 
> Thus a more sophisticated definition of thread safety for objects makes 
> a lot more sense that simply stating that practically all objects are 
> not thread safe, even though the practical use of a class could be to be 
> have an instance used by a single thread.
> 
> e.g. TList:
> 
> TThreadList is "completely thread safe".
> 
> TList is (supposedly) "per-instance thread safe": the "TThread.Add" 
> "piece of code"  can happily be used by multiple threads at the same 
> time, as long as each thread uses a different instance. I suppose this 
> is done quite often.

Please read a book about parallel programming.

Mattias




More information about the Lazarus mailing list