0

I am trying to perform a task on an interval and I made a POC using boost::asio::steady_timer.

It ends up there is nothing wrong with the timer.

Try the following test:

  • Run the program and do not touch the mouse
  • Observe the console window output updating every second

Now try this test:

  • Run the program again
  • Let it run a bit and observe the output updating every second
  • Set a breakpoint inside the timer callback
  • Left click on the console window
  • Observe that the timer callback no longer gets hit
  • Right click on the console window
  • Observe the program continues executing as normal

Can someone explain this behavior? Does std::cout block when you left click on the console window until you right click on it?

#include <boost/asio.hpp>
#include "boost/asio/deadline_timer.hpp"
#include<boost/bind.hpp>
#include <chrono>
#include <iostream>

class Pooper
{
    boost::asio::io_context & m_ioContext;
    boost::asio::steady_timer m_timer;

public:
    Pooper(boost::asio::io_context & ioContext)
        :
        m_ioContext(ioContext)
      , m_timer(boost::asio::steady_timer(ioContext))
    {
    }

    void Run()
    {
        m_timer.expires_from_now(std::chrono::seconds(1));
        m_timer.async_wait(boost::bind(&Pooper::OnTimerExpired, this, boost::asio::placeholders::error));
    }

    void OnTimerExpired(const boost::system::error_code & error)
    {
        if (error.failed())
        {
            std::cerr << "boost error: Failed" << std::endl;
        }

        std::cout << "Poop" << std::endl;

        try
        {
            // Do stuff here later

            m_timer.expires_from_now(std::chrono::seconds(1));
            m_timer.async_wait(boost::bind(&Pooper::OnTimerExpired, this, boost::asio::placeholders::error));
        }
        catch (std::exception &)
        {
            std::cerr << "An exception occured." << std::endl;
        }
    }
};

int main()
{
    boost::asio::io_context ioContext;
    auto pooper = Pooper(ioContext);
    pooper.Run();
    ioContext.run();

    std::cerr << "Exited..." << std::endl;
}

Using:

  • boost 1.70.0 from nuget along with the vc-142 libs
  • Visual Studio 2019
rafix07
  • 20,001
  • 3
  • 20
  • 33
Christopher Pisz
  • 3,757
  • 4
  • 29
  • 65
  • 3
    In Win10, the "Quick Edit" mode is set by default on `cmd.exe` windows. If when you left-click, you see a highlighted cell, that cell has been selected, and output is likely suspended until you hit Return or right-click to re-enable output. That my best guess without firing up windows to find out. So I doubt it is `cout` that is responsible. – David C. Rankin Jul 12 '19 at 03:39
  • @DavidC.Rankin, I have observed the behavior you describe on `cmd.exe` window. The same behavior occurs PowerShell terminal too. I imagine they share some common code base. – R Sahu Jul 12 '19 at 03:41
  • @MilesBudnek, right. It's not a problem always. – R Sahu Jul 12 '19 at 03:44
  • Lets flag it is a duplicate of https://stackoverflow.com/questions/30418886/how-and-why-does-quickedit-mode-in-command-prompt-freeze-applications which explains it quite well. I just didn't know to search for "quick edit". Thanks David! – Christopher Pisz Jul 12 '19 at 03:46
  • 2
    Possible duplicate of [How and why does QuickEdit mode in Command Prompt freeze applications?](https://stackoverflow.com/questions/30418886/how-and-why-does-quickedit-mode-in-command-prompt-freeze-applications) – Christopher Pisz Jul 12 '19 at 03:47
  • Possible duplicate of [C++ program stops producing console output upon input to the console](https://stackoverflow.com/questions/48927276/c-program-stops-producing-console-output-upon-input-to-the-console) – Scheff's Cat Jul 12 '19 at 05:00
  • @jam: The purpose of duplicates is, that answers get consolidated, with multiple questions pointing to them. This allows questions to be discovered by a wider set of questions. A question still represents value to future visitors, even if it is a duplicate. Deleting it amounts to destroying value. – IInspectable Jul 12 '19 at 06:48

0 Answers0