Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The code with the local and global typed constant does exactly the same thing. </p> <p>As David already stated the global static variable (aka typed constant) is reachable throughout the program and the local static var is not. Below I will refer to <code>typed constants</code> as <code>static variables</code> because that's what they really are.</p> <p>However the local static variable does persist in memory in exactly the same way as the global static var does. It is just the compiler that does not allow you access to a local var from outside the routine.<br> Also note that if your local static var is especially large (or you have very many of them) they will eat up a constant chunk of memory <em>(even though I cannot conceive of a scenario where this could be a problem)</em>.</p> <p>Because the static variable resides in a fixed location it is not thread safe. It effectively turns into a shared variable between all thread instances.<br> If the static var cannot be altered in a single CPU cycle (i.e. if it's bigger than an integer or if it's a complex type) than two threads can alter different parts of the variable at the same time, often leading to corruption. </p> <p>It it can be altered in a single cycle, e.g. a boolean or integer than you can never know whether the thread you are in is the one that changed it last or another one did which will lead to unpredictable results in most cases. </p> <p><strong>In short</strong><br> Using static vars in threaded code is a very bad idea unless you know exactly what you're doing. </p> <p>An exception to this might be an integer counter where you only increment and test to see if more than x executions have occurred.<br> Static vars generally unsuitable for passing messages between threads. </p> <p>If you want to share data between threads it's a better idea to use <code>threadvar</code>,<br> see: <a href="http://en.wikipedia.org/wiki/Thread-local_storage" rel="nofollow noreferrer">http://en.wikipedia.org/wiki/Thread-local_storage</a><br> and: <a href="https://stackoverflow.com/search?q=delphi+threadvar">https://stackoverflow.com/search?q=delphi+threadvar</a> </p> <p><strong>Finally</strong><br> There are very few problems that require global static vars and they are best avoided because they are dangerous.<br> Local static vars are useful in <strong>single threaded code</strong> to keep track of state between different executions of a routine.<br> They are useless for doing this in multi-threaded code because of race conditions. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload