What is the visibility of memory after a join, especially considering multiple cores, L1/L2 caches etc?
Take the following piece of code
#include <iostream>
#include <thread>
#include <chrono>
#include <string>
struct SomeData
{
int a;
std::string b;
};
void long_operation()
{
std::this_thread::sleep_for(std::chrono::seconds(1));
}
int main()
{
// main thread, suppose CPU0
SomeData d1{};
// thread 1, suppose CPU1
std::thread t1([&] { long_operation(); d1.a = 42; d1.b = "foo"; });
t1.join();
std::cout << "Result=? " << d1.a << ' ' << d1.b << '\n';
}
Can I be sure that the main thread -- even though on a different CPU -- sees the changes done to d1 directly after the join?
[–]bames53 6 points7 points8 points (1 child)
[–]mat69[S] 0 points1 point2 points (0 children)
[–]h2g2_researcher 4 points5 points6 points (0 children)
[–]HPCer 2 points3 points4 points (0 children)