Sometimes i get runtime error while using multimap std::async. Visual2019 in debug mode show this error:
Expression: cannot dereference end map/set iterator.
Example of code that generates error:
#include <iostream> #include <map> #include <future> #include <mutex> #include <Windows.h> class MyClass { public: MyClass() { mp.emplace(mapsize, 'f'); mapsize += 1; ft = std::async([this]() { mx.lock(); while (true) { for (int i = 0; i < mapsize; i++) { auto pr = mp.equal_range(i); for (auto j = pr.first; j != pr.second; j++) std::cout << j->second << "\n";}} mx.unlock(); }); } private: std::mutex mx; static int mapsize; std::future <void>ft; static std::multimap <int, char> mp; }; int MyClass::mapsize; std::multimap <int, char> MyClass::mp; int main() { for (int i = 0; i < 100000; i++) new MyClass(); } Edit: i have made some synchronization, but it still generates the same error