mardi 21 janvier 2020

Can high CPU usages avoided in below code

Below code Runs inside a thread. Start() method has endless while loop. Which run for ever and closes on destructor. I suspect ReceivedMsg() takes high cpu.

Problem: It takes high CPU. What will be efficient way to avoid high CPU.

void Start() 
{ 
  // close is true when close message is received
  while (!close) 
  {    
    if(!send_container_map->empty())
      SendMsg();

    if(!receive_container_map.empty())
      ReceivedMsg();
  } 
}

void SendMsg() 
{
  while (!send_container_map.empty()) 
  {
     //send the data
  }    
}

void receive_msg() 
{
  for (auto it = receive_msg.begin(); it != receive_msg.end(); ++it) 
  {
    receive_container_map.push_back(it->second); 
  }

  std::this_thread::sleep_for(std::chrono::microseconds(3));
  Read(Receiver_vector); // read the data 
}

Aucun commentaire:

Enregistrer un commentaire