Title: avoiding busy wait with boost181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16sio poll
Tags: c++;boost-asio;busy-waiting
Question: I'm writing a service on linux that uses boost181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16sio181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16io_service with io_servi181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16poll in a while loop.  This is a busy wait loop, e.g. it wastes CPU cycles.

```void Application181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16run()
{
    try
    {
        st181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16cout << "TcpServer starting..\n";
        _TcpServer.reset( new TcpServer(_io_service, boost181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16ref(_spState)) );

        // _io_service.run();

        while( ! _spState-&gt;QuitSignalled() )
        {
            _io_service.poll(); 
        }

        st181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16cerr << "quit signalled, TcpServer stopping.. :/" << st181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16endl;
    }
    catch(st181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16exception &amp; e)
    {
        st181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16cout << e.what() << "\n";
    }
}
```

I use poll instead of run to check if another thread in the service has signalled service shutdown.

Is there a way of achieving this without using poll in a busy wait loop ?

The service uses async io on a single thread, and other threads do data processing.

I've added a sleep between iterations of the loop which seems to reduce the waste of cpu time, but I was hoping there might be a more efficient way?

```void Application181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16run()
{
    using boost181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16this_thr181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16sleep_for;

    static const boost181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16hrono181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16milliseconds napMsecs( 50 );

    try
    {
        st181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16cout << "TcpServer starting..\n";
        _TcpServer.reset( new TcpServer(_io_service, boost181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16ref(_spState)) );

        // _io_service.run();

        while( ! _spState-&gt;QuitSignalled() )
        {
            _io_service.poll();
            boost181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16this_thr181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16sleep_for( napMsecs );
        }

        st181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16cerr << "quit signalled, TcpServer stopping.. :/" << st181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16endl;
    }
    catch(st181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16exception &amp; e)
    {
        st181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16cout << e.what() << "\n";
    }
}
```


Here is the accepted answer: I'd say, simply take advantage of the fact that ```boost181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16sio181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16io_service``` is fully threadsafe by default, and do

```iosvc.run();
```

And signal service shutdown on "another thread in the service" like you would:

```iosvc.stop();
```

Remember to ```iosvc.reset()``` before you call ```{run,poll}[_one]``` again, as per the documentation.

Of course you can also use other means to signal the actual logical workers to end, but then that's completely independent unrelated to Boost Asio

Comment for this answer: Actually, since `io_service` is threadsafe, you can just _call_ the method instead of posting it. I had a bit of a brainfart there
Comment for this answer: I'm restricted to using c++98 and boost 1_47, but I'll have a look into io_service.post to see if it can do the equivalent with a function object.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2014:00:10/09/14', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c++><boost-asio><busy-waiting>', 'Title': 'avoiding busy wait with boost::asio poll', 'ViewCount': 1902, 'id': 26272185, 'user_AboutMe': None, 'user_AccountId': 5066439.0, 'user_DisplayName': 'stack user', 'user_Reputation': 797.0, 'user_Views': 63.0, 'user_id': 4066425.0}}