Supposedly I have a large std::map<SomeType1, std::vector<SomeType2>> my_map
, and I need to sort all vectors in the map. Currently I'm running in a single thread:
for (auto& item : my_map)
{
std::sort(item.second.begin(), item.second.end(), &some_comparer);
}
Using the above code, my CPU is constantly idle around ~15%, so I think I can divide the map into smaller sections and sort each sections in seperate threads.
I'd like to ask, how can I divide a map? For example, I want to split it into 4 sections:
auto& section1 = my_map.divide(0, 0.25); // <~ how to apply this?
auto& section2 = my_map.divide(0.25, 0.5);
auto& section1 = my_map.divide(0.5, 0.75);
auto& section1 = my_map.divide(0.5, 1);
std::thread thread1([§ion1] { sort_for_me_pls(section1); });
std::thread thread2([§ion2] { sort_for_me_pls(section2); });
std::thread thread3([§ion3] { sort_for_me_pls(section3); });
std::thread thread4([§ion4] { sort_for_me_pls(section4); });
thread1.join();
thread2.join();
thread3.join();
thread4.join();