Does cPP loop when traversing map?

Club * ClubMgr::GetClub(clubid_t nClubId) const
{
    for (std::map<userid_t, std::map<clubid_t, Club* > >::const_iterator it1 = m_hashClubs.begin(); it1 != m_hashClubs.end(); PPit1)
    {
        std::map<clubid_t, Club* >::const_iterator it2 = it1->second.find(nClubId);
        
        if (it2 != it1->second.end())
        {
            return it2->second;
        }
    }
    return NULL;
}

the code of the endless loop, as above, is always in an endless loop after running online for more than a day. Cpu100%, can"t find the reason, so it"s not convenient to post all the codes. I just want to ask you, what will happen in the cycle of execution in map?

ps: Oh yes, I forgot to add that the business part is single-threaded.

CPP
Mar.11,2021

  1. cpu causes 100% only when doing intensive calculations. The most common example is that too many loops are executed, or it is that kind of endless loop.
  2. back to your code, there is no logic problem with your function, but there is a performance problem. When the number of your outer map elements reaches 100000 or even millions, the loop needs to be executed many times before it can exit.
  3. Optimization: either add and modify query conditions, add a userid, query only need to do two map find operations, or modify the stored data structure to improve query efficiency.

linux will have a reason to die, to see if message has any related printing of oom killer. If it is an endless loop, you can only check where it may be by comparing svn and so on.

if it is an error or other problem, the linux kernel will capture coredump or crash (vmcore information accordingly. If you just look at the code this way, I think it's difficult to find the reason, and you have to use the appropriate tools.

if there is an endless loop, linux will be recorded in / var/log/message. You can see that when memory is tight, message,oom kiiler will in turn kill processes with high memory consumption, and some information such as pid,process name,cpu mask,trace will be recorded. Similar problems can be found through monitoring.


is awesome. Traversing map


purely logically, the it1 that affects the number of for loops only changes the value at PPit1, and there should be no endless loop. The 100% reason why CPU is running may be somewhere else.


code, no problem, generally like this problem, do not manually write loop traversal, try to use the algorithm method in-sharpinclude , such as find_if ().
and before entering this function, print the m_hashClubs.size (), to confirm whether the size is too large, and then check the reason why the size is too large.
there are very few people who can traverse map. Map is for fast indexing. If you want to traverse, why do you need map? Modify the design.


userid_t and clubid_t operator < how are they defined?

in addition, there may be problems with the complexity of your algorithm

Do you have an answer to the question

? I seem to have a similar problem

Menu