When running this code thread sanitizer complains about data race. Why?
#include <iostream>
#include <ranges>
#include <thread>
#include <vector>
int main(){
std::vector v{11,22,33,44,55,66};
auto view = v | std::ranges::views::filter([](const auto x){
return x>47;
});
std::jthread jt1([&]{
int sum =0;
for (const auto& val: view) {sum+=val;}
});
std::jthread jt2([&]{
int sum =0;
for (const auto& val: view) {sum+=val;}
});
}
note: I know answer to this, I learned it recently watching a talk, but I found it very surprising and no existing questions related to this, so I wanted to share it. If nobody is interested in answering I will answer it myself.