How can I protect the vector v from crashing? And another question, why is not it already crashed, shouldn't it?
#include <Windows.h> #include <thread> #include <vector> #include <algorithm> #include <iostream> using namespace std; vector<int> v; void a() { while (true) { v.push_back(1); Sleep(100); } } void b() { while (true) { if (!v.empty()) { v.erase(v.begin()); } Sleep(100); } } void c() { while (true) { v.push_back(1); Sleep(100); } } int main() { thread(&a).detach(); thread(&b).detach(); thread(&c).detach(); while (true) { for (int i = 0; i < v.size(); i++) { v[i]++; } cout << v.size() << endl; if (!v.empty()) v.erase(v.begin()); Sleep(100); } }