Are read from and writing to vector thread-safe operations in vector C++? [duplicate]
问题 This question already has answers here : std::vector, thread-safety, multi-threading (3 answers) Closed 9 months ago . I have a below code that gives segmentation fault sometime? vector<int> myvector; void function1() { for(int i = 0;i<10;i++) { cout<<"writer thread:"<<i<<endl; myvector.push_back(i); } } void function2() { for(int i = 0;i<10;i++) { cout<<"reader thread:"; cout<<myvector[i]<<endl; } } int main() { thread t1(function1); thread t2(function2); t1.join(); t2.join(); } I am little