Avoid busy wait

Jan 7, 2016 at 7:54 AM
I have implemented rest server and opened up couple of apis on it. my listener code looks like this.
http_listener listener("http://0.0.0.0:8000/render");

listener.support(methods::GET, handle_get);
listener.support(methods::POST, handle_post);


try
{
    listener
    .open()
    .then([&listener](){TRACE("\nstarting to listen\n");})
    .wait();

    while (true);
}
the issue with this code is the busy wait at last line of code. this code basically hogs one of the cpus. is there a way i can avoid this busy wait.
Mar 9, 2016 at 2:29 PM
I'm having this same issue as well.

I've tried to add some Sleeps, but have been unable to do so successfully.

Anyone have a solution to avoid that taking up 25% of the CPU?

My code looks like
int main()
{


   Sleep(1);
    
   http_listener listener(L"http://localhost:80");         //sets my listener to an address, can be anything - but for now the example is listener(L"http://localhost:80"); 

   Sleep(1);
 
   listener.support(methods::GET, handle_get);
    Sleep(1);
   listener.support(methods::POST, handle_post);
    Sleep(1);
   listener.support(methods::PUT, handle_put);

   Sleep(1);
 
   try
   {
      Sleep(1);
    
      listener
         .open()
         .then([&listener](){TRACE(L"\nstarting to listen\n");})
         .wait();


    
        Sleep(1);
    
      while (true);
   }
   catch (exception const & e)
   {
      wcout << e.what() << endl;
   }
  
  Sleep(1);

   return 0;
}
Mar 9, 2016 at 2:34 PM
Edited Mar 9, 2016 at 2:34 PM
I have updated my code to following this solves the issue.
http_listener listener("http://0.0.0.0:8000/render");

listener.support(methods::GET, handle_get);
listener.support(methods::POST, handle_post);


try
{
    listener
   .open()
   .then([&listener](){TRACE("\nstarting to listen\n");})
   .wait();

   while (true){
        sleep(10000);
   }
}
Mar 9, 2016 at 3:24 PM
Thanks gaurav_123qwe!

I updated my code the same way and it worked, albeit I only used Sleep(1);