title: Is it Thread Safe to read from lower index elements of an struct array while it is being filled with data in main thread
body: <p><strong>Original Question:</strong></p>
  
  <p>I got an array of structs and filling it in a separate thread while reading it in main thread:</p>
  
  <pre><code>struct DataModel MyData[1024];
  
  struct DataModel
  {
      bool IsFilled;
      float a;
      float b;
  }
  </code></pre>
  
  <ul>
  <li><p>I have a Thread which is filling the <code>Mydata</code> array from 0 index to the last index(in above it is 1024).</p></li>
  <li><p>Then i get the last filled struct index out from the filling thread.</p></li>
  <li><p>And then i try to read the the values of the element with one index lower than the filled one.</p></li>
  <li><p>Let 's assume that when the 500th element is filled i read the value from 499 element of <code>MyData</code> array, so i assure that i am not reading the array element which is being written.</p></li>
  </ul>
  
  <p><strong>Q1:</strong> Is this thread safe? </p>
  
  <p><strong>Q2:</strong> is there chance of occurring undefined behavior or misreading the vales?</p>
  
  <hr>
  
  <p><strong>Further Edits:</strong></p>
  
  <p>The question was improperly edited to add more details and that's why it introduced inconsistency over answers so i separated the previous edits to improve consistency over answers and accepted answer.</p>
  
  <p><strong>Edit 1</strong>:
  this is a suggestion of the possible implementation. though it may show false results, but just i wanted to ask about thread-safety and and undefined behavior, the following solution may show various result but i was trying to ask about thread safety first.</p>
  
  <pre><code>std::atomic&lt;int&gt; FilledIndex;
      void FillingMyData(struct DataModel myData[])
      {
        for(size_t i = 0; i &lt; 1024; i++)
        {
          myData[i].a = rand();
          myData[i].b = rand();
          myData[i].IsFilled = true;
  
      FilledIndex = i;
    }
  }
  
  int main()
  {
       std::thread ReadThread(FillingMyData, MyData);
       while(FilledIndex &lt; 1024)
       {
            std::cout &lt;&lt; MyData[FilledIndex].a;
       }
       ReadThread.join();
       return 0;
  }
  </code></pre>