What is the C++11 memory model and how does it affect multithreaded programming?
Hello everyone. I'm learning C++ and have reached multithreading. I read that C++11 introduced some kind of "memory model" that changed everything. But I can't understand what it actually means and why it's needed.\n\nPreviously I wrote something like:\n\n```cpp\n#include \n#include \n#include \n\nint shared_data = 0;\nbool ready = false;\n\nvoid writer() {\n shared_data = 42; // Step 1\n ready = true; // Step 2\n}\n\nvoid reader() {\n while (!ready); // Wait until ready becomes true\n std::cout << shared_data << std::endl; // Will this always be 42?\n}\n```\n\nI ran this on my computer and it worked. But I was told that without the memory model and atomic operations with proper barriers, this code could break on a different processor or with compiler optimizations.
No answers yet.
Log in to leave an answer