thread local - what is the advantage of making std::vector thread_local? -
seeing crash in erase()/pop_back() in multithreaded enviroment, looks iterator doesn't invalidated. advantage of thread_local in defining vector, iterators have invalidation problem in case.
#include <iostream> #include <vector> #include <iterator> #include <assert.h> #include <thread> thread_local std::vector<int> v; void threadfuncerase(std::vector<int>::iterator i){ std::cout << "threadfuncerase"<< std::endl; v.erase(i); } void threadfuncpop(){ std::cout << "threadfuncpop"<< std::endl; v.pop_back(); } int main() { v.push_back(1); v.push_back(2); std::vector<int>::iterator i1 = v.begin(), i2 = v.end() - 1, i3 = v.begin() + 1; std::cout << "main"<< std::endl; std::thread t1(threadfuncpop); std::thread t2(threadfuncerase, /*i2*/v.end() - 1); //std::thread t2(threadfuncpop); t1.join(); t2.join(); }
Comments
Post a Comment