<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta content="text/html; charset=UTF-8" http-equiv="Content-Type"/>
    <title></title>
  </head>

  <body>
    <p style="margin: 0px;"><span> </span></p>

    <p style="margin: 0px;"> </p>

    <div style="margin: 5px 0px 5px 0px;">
      <br/>
       Martin <lazarus@mfriebe.de> hat am 27. Juni 2011 um 16:01 geschrieben:<br/>
       [...] 

      <blockquote style="margin-left: 0px; padding-left: 10px; border-left: solid 1px blue;" type="cite">
        <blockquote type="cite">
          <p style="margin: 0px;">Think about "safely invoked" as no need for extra precautions to call it multi threaded, like e.g. critical sections or check which thread is running. </p>

          <p style="margin: 0px;">Obviously "thread-safe" does not extend the safety nor does it fix any kind of flaw that is already there when called single threaded. </p>

          <p style="margin: 0px;">And "thread safe" does not mean that calling a function multi threaded will give the same results as called single threaded. Although typically it does. </p>

          <p style="margin: 0px;"> </p>

          <p style="margin: 0px;">And look up the definition of "function". A function is defined by the input, output and task. Failing means for some input the wrong output is returned. Input/Output are not only parameters.</p>
        </blockquote><br/>
         Yes, but if I am correct, "input" for example can not (always) be limited to the list of param (not even including hidden param, like self)<br/>
         eg, a constructor, includes the task of allocating memory. If that fails a defined behaviour should be expected (return nil, throw exception, ...). That implies, that "available memory" in this case is part of the input.
      </blockquote>
    </div>

    <p style="margin: 0px;">In fact a function's input can even be a file on disk. </p>

    <p style="margin: 0px;">The memory manager is thread safe, so there is not much difference between single threaded and multi threaded here. </p>

    <p style="margin: 0px;"> </p>

    <p style="margin: 0px;"> </p>

    <div style="margin: 5px 0px 5px 0px;">
      <blockquote style="margin-left: 0px; padding-left: 10px; border-left: solid 1px blue;" type="cite">
        <br/>
         Sure, similar flaws (with invalid data) can be constructed outside threads. But the ability to store data in either a variable local to one thread, or global to all threads does not exists without threads.
      </blockquote>
    </div>

    <p style="margin: 0px;">Well, this depends on pov. So this is more a philosophical question. </p>

    <p style="margin: 0px;"> </p>

    <div style="margin: 5px 0px 5px 0px;">
      <blockquote style="margin-left: 0px; padding-left: 10px; border-left: solid 1px blue;" type="cite">
        <br/>
         Yes the aim of the definition, is solely to extend the concept of being called safely to being called safely from several threads.<br/>
         But the above definition *limits* this requirement to "called at the same time"
      </blockquote>
    </div>

    <p style="margin: 0px;">English lacks the exactness of mathematical expressions.  </p>

    <p style="margin: 0px;">You are right, that the definition is not exact and the wikipedia page contains a few remarks about that.</p>

    <p style="margin: 0px;">The nice thing about this definition is that everyone I knew instantly understands the idea and was able to apply it to most cases without much thinking, including methods and classes. Everyone - until I read this thread. Apparently some people have problems with it. Well, life is learning. </p>

    <p style="margin: 0px;"> </p>

    <p style="margin: 0px;"> </p>

    <div style="margin: 5px 0px 5px 0px;">
      <blockquote style="margin-left: 0px; padding-left: 10px; border-left: solid 1px blue;" type="cite">
        <blockquote type="cite">
          <br/>

          <p style="margin: 0px;">The "fail" depends on how you define the task of the function.</p>

          <p style="margin: 0px;">I guess with "fail" you mean the function can fail its tasks when called multi threaded. Then it is by definition not thread safe.</p>
        </blockquote><br/>
        <br/>
         As I said, those examples, are not actually correct (for what I am questioning) They only point into a direction.<br/>
        <br/>
         fail = not the result that is expected according to the definition of the function (that could be: not return at all, crash, or return an incorrect value as result...)
      </blockquote>
    </div>

    <p style="margin: 0px;"> </p>

    <p style="margin: 0px;">Then the function is not thread safe. </p>

    <p style="margin: 0px;"> </p>

    <div style="margin: 5px 0px 5px 0px;">
      <blockquote style="margin-left: 0px; padding-left: 10px; border-left: solid 1px blue;" type="cite">
        <br/>
         The example is merely to indicate, that thread related problems can occur, even if calls are "not at the same time".
      </blockquote>
    </div>

    <p style="margin: 0px;">True.  </p>

    <p style="margin: 0px;">And I would like to discuss this, but I'm not sure if this topic is Lazarus related. </p>
    <br/>

    <p style="margin: 0px;"> </p>

    <div style="margin: 5px 0px 5px 0px;">
      <blockquote style="margin-left: 0px; padding-left: 10px; border-left: solid 1px blue;" type="cite">
        [...] 

        <blockquote type="cite">
          <br/>

          <p style="margin: 0px;">"At the same time" includes "subsequently", for the simple reason that critical sections are allowed, which simply turns a parallel (at the same time) into a sequential one.</p>
        </blockquote><br/>
         There is a difference between called/invoked at the same time, and executed at the same time.
      </blockquote>
    </div>

    <p style="margin: 0px;">For "thread safe" it is the same. </p>

    <p style="margin: 0px;"> </p>

    <p style="margin: 0px;"> </p>

    <div style="margin: 5px 0px 5px 0px;">
      <blockquote style="margin-left: 0px; padding-left: 10px; border-left: solid 1px blue;" type="cite">
        <br/>
         The original defintion says "invoked" they are still invoked at the same time, even though *if* a critical section is used, the part inside it (and only this part, not the whole piece of code, which includes the critical section) is executed deferred.<br/>
        <br/>
         "at the same time" does not necessarily mean a critical section has to exist, there are other means that allow for more parallel execution:<br/>
          2 read only calls, may not need to block each other at all. Yet a read-only  call may fail due to something thread related, that happened long before (in an other thread, and only because it happened in an other thread).<br/>
        <br/>
      </blockquote>
    </div>

    <p style="margin: 0px;">Yes. But then again it is a question of how you define the task of the code. Maybe you want this behavior, maybe you don't care. </p>

    <p style="margin: 0px;"> </p>

    <p style="margin: 0px;"> </p>

    <p style="margin: 0px;">Mattias</p>

    <p style="margin: 0px;"> </p>
  </body>
</html>