Auto optimisation for L cache for object's variables?










2















Frankly, this is a continue of this my question, inspired by this answer: https://stackoverflow.com/a/53262717/1479414



Let's suppose we have a class:



public class Foo 
private Integer x;

public void setX(Integer x)
this.x = x;


public Integer getX()
return this.x;




And let us consider a very specific scenario, when we have just two threads which interact with the x variable:



At time 1, a thread T1 is created



At time 2, T1 sets the value: foo.setX(123);



At time 3, a thread T2 is created



At time 4, T2 reads the value: foo.getX();



No other threads interact with this value. Both operations happen only once.



So there is no apparent x value read operations before the thread T2 does his job.



The question is: does any auto optimisation for L cache exist which can cache null value of the x variable, so the thread T2 will read its cached value?
In other words, do we need the volatile modifier in this particular scenario?










share|improve this question




























    2















    Frankly, this is a continue of this my question, inspired by this answer: https://stackoverflow.com/a/53262717/1479414



    Let's suppose we have a class:



    public class Foo 
    private Integer x;

    public void setX(Integer x)
    this.x = x;


    public Integer getX()
    return this.x;




    And let us consider a very specific scenario, when we have just two threads which interact with the x variable:



    At time 1, a thread T1 is created



    At time 2, T1 sets the value: foo.setX(123);



    At time 3, a thread T2 is created



    At time 4, T2 reads the value: foo.getX();



    No other threads interact with this value. Both operations happen only once.



    So there is no apparent x value read operations before the thread T2 does his job.



    The question is: does any auto optimisation for L cache exist which can cache null value of the x variable, so the thread T2 will read its cached value?
    In other words, do we need the volatile modifier in this particular scenario?










    share|improve this question


























      2












      2








      2








      Frankly, this is a continue of this my question, inspired by this answer: https://stackoverflow.com/a/53262717/1479414



      Let's suppose we have a class:



      public class Foo 
      private Integer x;

      public void setX(Integer x)
      this.x = x;


      public Integer getX()
      return this.x;




      And let us consider a very specific scenario, when we have just two threads which interact with the x variable:



      At time 1, a thread T1 is created



      At time 2, T1 sets the value: foo.setX(123);



      At time 3, a thread T2 is created



      At time 4, T2 reads the value: foo.getX();



      No other threads interact with this value. Both operations happen only once.



      So there is no apparent x value read operations before the thread T2 does his job.



      The question is: does any auto optimisation for L cache exist which can cache null value of the x variable, so the thread T2 will read its cached value?
      In other words, do we need the volatile modifier in this particular scenario?










      share|improve this question
















      Frankly, this is a continue of this my question, inspired by this answer: https://stackoverflow.com/a/53262717/1479414



      Let's suppose we have a class:



      public class Foo 
      private Integer x;

      public void setX(Integer x)
      this.x = x;


      public Integer getX()
      return this.x;




      And let us consider a very specific scenario, when we have just two threads which interact with the x variable:



      At time 1, a thread T1 is created



      At time 2, T1 sets the value: foo.setX(123);



      At time 3, a thread T2 is created



      At time 4, T2 reads the value: foo.getX();



      No other threads interact with this value. Both operations happen only once.



      So there is no apparent x value read operations before the thread T2 does his job.



      The question is: does any auto optimisation for L cache exist which can cache null value of the x variable, so the thread T2 will read its cached value?
      In other words, do we need the volatile modifier in this particular scenario?







      java multithreading volatile cpu-cache






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 '18 at 14:07







      Andremoniy

















      asked Nov 12 '18 at 13:50









      AndremoniyAndremoniy

      21.8k674158




      21.8k674158






















          2 Answers
          2






          active

          oldest

          votes


















          2














          When you create a thread, it will see any value set before it is created.



          In the Javadoc for java.util.concurrency for Java 11 under Memory Visibility Properties and JLS-17.4.5 for Java 8 states




          A call to start on a thread happens-before any action in the started thread.




          NOTE: A thread only reads a cached value, when it re-reads a value in a cache line it has already in its cache. If it reads a cache line it has never read before or is no longer in a cache, it won't read a stale value.






          share|improve this answer

























          • Thank you a lot for your answer! It sounds logical that when a new thread is created it should see the actualised value of any variable. However, could this statement be strengthened with some links to the specification?

            – Andremoniy
            Nov 12 '18 at 19:03






          • 1





            @Andremoniy I have added a quote.

            – Peter Lawrey
            Nov 12 '18 at 19:19


















          1














          T1 and T2 are executed sequentially and cache is coherent, especially in this sequential use case.



          So there is no way that T2 at time 4 will get a null value.






          share|improve this answer























          • So you assume that the CPU cache has nothing to do with volatile-problem?

            – Andremoniy
            Nov 12 '18 at 15:12












          • I do not assume that. I assume that because the execution of T1 and T2 is sequential, the cache will be coherent (no concurrency issue)

            – Nicolas Labrot
            Nov 12 '18 at 15:31











          • That was my question: some people claim that there exist an auto optimisaiton which acts independently of existent threads

            – Andremoniy
            Nov 12 '18 at 15:37






          • 1





            Could you please elaborate?

            – Nicolas Labrot
            Nov 12 '18 at 16:31











          • I don't know how to elaborate. It was said that there is a "auto optimisation for L* cache" which can cause the null value be cached and passed therefore to the thread T2. That was my question: does something similar really exist?

            – Andremoniy
            Nov 12 '18 at 19:04










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53263594%2fauto-optimisation-for-l-cache-for-objects-variables%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          When you create a thread, it will see any value set before it is created.



          In the Javadoc for java.util.concurrency for Java 11 under Memory Visibility Properties and JLS-17.4.5 for Java 8 states




          A call to start on a thread happens-before any action in the started thread.




          NOTE: A thread only reads a cached value, when it re-reads a value in a cache line it has already in its cache. If it reads a cache line it has never read before or is no longer in a cache, it won't read a stale value.






          share|improve this answer

























          • Thank you a lot for your answer! It sounds logical that when a new thread is created it should see the actualised value of any variable. However, could this statement be strengthened with some links to the specification?

            – Andremoniy
            Nov 12 '18 at 19:03






          • 1





            @Andremoniy I have added a quote.

            – Peter Lawrey
            Nov 12 '18 at 19:19















          2














          When you create a thread, it will see any value set before it is created.



          In the Javadoc for java.util.concurrency for Java 11 under Memory Visibility Properties and JLS-17.4.5 for Java 8 states




          A call to start on a thread happens-before any action in the started thread.




          NOTE: A thread only reads a cached value, when it re-reads a value in a cache line it has already in its cache. If it reads a cache line it has never read before or is no longer in a cache, it won't read a stale value.






          share|improve this answer

























          • Thank you a lot for your answer! It sounds logical that when a new thread is created it should see the actualised value of any variable. However, could this statement be strengthened with some links to the specification?

            – Andremoniy
            Nov 12 '18 at 19:03






          • 1





            @Andremoniy I have added a quote.

            – Peter Lawrey
            Nov 12 '18 at 19:19













          2












          2








          2







          When you create a thread, it will see any value set before it is created.



          In the Javadoc for java.util.concurrency for Java 11 under Memory Visibility Properties and JLS-17.4.5 for Java 8 states




          A call to start on a thread happens-before any action in the started thread.




          NOTE: A thread only reads a cached value, when it re-reads a value in a cache line it has already in its cache. If it reads a cache line it has never read before or is no longer in a cache, it won't read a stale value.






          share|improve this answer















          When you create a thread, it will see any value set before it is created.



          In the Javadoc for java.util.concurrency for Java 11 under Memory Visibility Properties and JLS-17.4.5 for Java 8 states




          A call to start on a thread happens-before any action in the started thread.




          NOTE: A thread only reads a cached value, when it re-reads a value in a cache line it has already in its cache. If it reads a cache line it has never read before or is no longer in a cache, it won't read a stale value.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 12 '18 at 19:19

























          answered Nov 12 '18 at 18:39









          Peter LawreyPeter Lawrey

          442k56559965




          442k56559965












          • Thank you a lot for your answer! It sounds logical that when a new thread is created it should see the actualised value of any variable. However, could this statement be strengthened with some links to the specification?

            – Andremoniy
            Nov 12 '18 at 19:03






          • 1





            @Andremoniy I have added a quote.

            – Peter Lawrey
            Nov 12 '18 at 19:19

















          • Thank you a lot for your answer! It sounds logical that when a new thread is created it should see the actualised value of any variable. However, could this statement be strengthened with some links to the specification?

            – Andremoniy
            Nov 12 '18 at 19:03






          • 1





            @Andremoniy I have added a quote.

            – Peter Lawrey
            Nov 12 '18 at 19:19
















          Thank you a lot for your answer! It sounds logical that when a new thread is created it should see the actualised value of any variable. However, could this statement be strengthened with some links to the specification?

          – Andremoniy
          Nov 12 '18 at 19:03





          Thank you a lot for your answer! It sounds logical that when a new thread is created it should see the actualised value of any variable. However, could this statement be strengthened with some links to the specification?

          – Andremoniy
          Nov 12 '18 at 19:03




          1




          1





          @Andremoniy I have added a quote.

          – Peter Lawrey
          Nov 12 '18 at 19:19





          @Andremoniy I have added a quote.

          – Peter Lawrey
          Nov 12 '18 at 19:19













          1














          T1 and T2 are executed sequentially and cache is coherent, especially in this sequential use case.



          So there is no way that T2 at time 4 will get a null value.






          share|improve this answer























          • So you assume that the CPU cache has nothing to do with volatile-problem?

            – Andremoniy
            Nov 12 '18 at 15:12












          • I do not assume that. I assume that because the execution of T1 and T2 is sequential, the cache will be coherent (no concurrency issue)

            – Nicolas Labrot
            Nov 12 '18 at 15:31











          • That was my question: some people claim that there exist an auto optimisaiton which acts independently of existent threads

            – Andremoniy
            Nov 12 '18 at 15:37






          • 1





            Could you please elaborate?

            – Nicolas Labrot
            Nov 12 '18 at 16:31











          • I don't know how to elaborate. It was said that there is a "auto optimisation for L* cache" which can cause the null value be cached and passed therefore to the thread T2. That was my question: does something similar really exist?

            – Andremoniy
            Nov 12 '18 at 19:04















          1














          T1 and T2 are executed sequentially and cache is coherent, especially in this sequential use case.



          So there is no way that T2 at time 4 will get a null value.






          share|improve this answer























          • So you assume that the CPU cache has nothing to do with volatile-problem?

            – Andremoniy
            Nov 12 '18 at 15:12












          • I do not assume that. I assume that because the execution of T1 and T2 is sequential, the cache will be coherent (no concurrency issue)

            – Nicolas Labrot
            Nov 12 '18 at 15:31











          • That was my question: some people claim that there exist an auto optimisaiton which acts independently of existent threads

            – Andremoniy
            Nov 12 '18 at 15:37






          • 1





            Could you please elaborate?

            – Nicolas Labrot
            Nov 12 '18 at 16:31











          • I don't know how to elaborate. It was said that there is a "auto optimisation for L* cache" which can cause the null value be cached and passed therefore to the thread T2. That was my question: does something similar really exist?

            – Andremoniy
            Nov 12 '18 at 19:04













          1












          1








          1







          T1 and T2 are executed sequentially and cache is coherent, especially in this sequential use case.



          So there is no way that T2 at time 4 will get a null value.






          share|improve this answer













          T1 and T2 are executed sequentially and cache is coherent, especially in this sequential use case.



          So there is no way that T2 at time 4 will get a null value.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 12 '18 at 14:38









          Nicolas LabrotNicolas Labrot

          2,8621828




          2,8621828












          • So you assume that the CPU cache has nothing to do with volatile-problem?

            – Andremoniy
            Nov 12 '18 at 15:12












          • I do not assume that. I assume that because the execution of T1 and T2 is sequential, the cache will be coherent (no concurrency issue)

            – Nicolas Labrot
            Nov 12 '18 at 15:31











          • That was my question: some people claim that there exist an auto optimisaiton which acts independently of existent threads

            – Andremoniy
            Nov 12 '18 at 15:37






          • 1





            Could you please elaborate?

            – Nicolas Labrot
            Nov 12 '18 at 16:31











          • I don't know how to elaborate. It was said that there is a "auto optimisation for L* cache" which can cause the null value be cached and passed therefore to the thread T2. That was my question: does something similar really exist?

            – Andremoniy
            Nov 12 '18 at 19:04

















          • So you assume that the CPU cache has nothing to do with volatile-problem?

            – Andremoniy
            Nov 12 '18 at 15:12












          • I do not assume that. I assume that because the execution of T1 and T2 is sequential, the cache will be coherent (no concurrency issue)

            – Nicolas Labrot
            Nov 12 '18 at 15:31











          • That was my question: some people claim that there exist an auto optimisaiton which acts independently of existent threads

            – Andremoniy
            Nov 12 '18 at 15:37






          • 1





            Could you please elaborate?

            – Nicolas Labrot
            Nov 12 '18 at 16:31











          • I don't know how to elaborate. It was said that there is a "auto optimisation for L* cache" which can cause the null value be cached and passed therefore to the thread T2. That was my question: does something similar really exist?

            – Andremoniy
            Nov 12 '18 at 19:04
















          So you assume that the CPU cache has nothing to do with volatile-problem?

          – Andremoniy
          Nov 12 '18 at 15:12






          So you assume that the CPU cache has nothing to do with volatile-problem?

          – Andremoniy
          Nov 12 '18 at 15:12














          I do not assume that. I assume that because the execution of T1 and T2 is sequential, the cache will be coherent (no concurrency issue)

          – Nicolas Labrot
          Nov 12 '18 at 15:31





          I do not assume that. I assume that because the execution of T1 and T2 is sequential, the cache will be coherent (no concurrency issue)

          – Nicolas Labrot
          Nov 12 '18 at 15:31













          That was my question: some people claim that there exist an auto optimisaiton which acts independently of existent threads

          – Andremoniy
          Nov 12 '18 at 15:37





          That was my question: some people claim that there exist an auto optimisaiton which acts independently of existent threads

          – Andremoniy
          Nov 12 '18 at 15:37




          1




          1





          Could you please elaborate?

          – Nicolas Labrot
          Nov 12 '18 at 16:31





          Could you please elaborate?

          – Nicolas Labrot
          Nov 12 '18 at 16:31













          I don't know how to elaborate. It was said that there is a "auto optimisation for L* cache" which can cause the null value be cached and passed therefore to the thread T2. That was my question: does something similar really exist?

          – Andremoniy
          Nov 12 '18 at 19:04





          I don't know how to elaborate. It was said that there is a "auto optimisation for L* cache" which can cause the null value be cached and passed therefore to the thread T2. That was my question: does something similar really exist?

          – Andremoniy
          Nov 12 '18 at 19:04

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53263594%2fauto-optimisation-for-l-cache-for-objects-variables%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo