Original Question:
I got an array of structs and filling it in a separate thread while reading it in main thread:
struct DataModel MyData[1024];
struct DataModel
{
bool IsFilled;
float a;
float b;
}
I have a Thread which is filling the
Mydataarray from 0 index to the last index(in above it is 1024).Then i get the last filled struct index out from the filling thread.
And then i try to read the the values of the element with one index lower than the filled one.
Let 's assume that when the 500th element is filled i read the value from 499 element of
MyDataarray, so i assure that i am not reading the array element which is being written.
Q1: Is this thread safe?
Q2: is there chance of occurring undefined behavior or misreading the vales?
Further Edits:
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.
Edit 1: 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.
std::atomic<int> FilledIndex;
void FillingMyData(struct DataModel myData[])
{
for(size_t i = 0; i < 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 < 1024)
{
std::cout << MyData[FilledIndex].a;
}
ReadThread.join();
return 0;
}