3

I need to implement a function that should do specified task for specified duration which is passed to it as parameter (std::chrono::milliseconds).

I have come up with code :

void Run(std::chrono::milliseconds ms)
{
    std::chrono::time_point<std::chrono::system_clock> start, end;
    start = std::chrono::system_clock::now();
    std::chrono::duration<double> elapsed_seconds = end - start;
    while (elapsed_seconds <= (ms / 1000))
    {
        std::cout << "Running" << std::endl;
        end = std::chrono::system_clock::now();
        elapsed_seconds = end - start;
    }
}

int main()
{
    {
        std::chrono::milliseconds ms(30000);
        Run(ms);
        system("Pause");
    }

I suppose the code to print Running for 30 seconds and then exit. But it does not do so. How do I achieve such behavior with C++ <chrono>

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
Gaurav K
  • 2,864
  • 9
  • 39
  • 68
  • 3
    `std::future::wait_for` or `std::future::wait_until` maybe would be more appropriate? – Hiura Feb 27 '15 at 15:09
  • 11
    end has no value! could it be the source of your problem? – mans Feb 27 '15 at 15:09
  • 2
    @mans Yet another good reason not to declare more than one variable per line **and** always initializing your variables. – Borgleader Feb 27 '15 at 15:11
  • @Yakk elapsed_seconds showed value in seconds the example I referred, anyways I am trying with `std::future::wait_for` – Gaurav K Feb 27 '15 at 15:25
  • `ms / 1000` is 30ms, not 30s as you seem to think. And `elapsed_seconds` is a duration, not a number of seconds. Give them less misleading names like `elapsed` and `total` (not specifying units, since the point of `duration` is to handle units for you), and run while `elapsed < total`. – Mike Seymour Feb 27 '15 at 15:28

3 Answers3

6

All you really need is to know the end point and then loop until that point is reached:

#include <chrono>

void Run(std::chrono::milliseconds ms)
{
    std::chrono::time_point<std::chrono::system_clock> end;

    end = std::chrono::system_clock::now() + ms; // this is the end point

    while(std::chrono::system_clock::now() < end) // still less than the end?
    {
        std::cout << "Running" << std::endl;
    }
}

int main()
{
    std::chrono::milliseconds ms(3000);
    Run(ms);
    system("Pause");
}
Galik
  • 47,303
  • 4
  • 80
  • 117
2

Remove the variable end. You fail to initialize it anyhow.

Replace all uses of it with std::chrono::system_clock::now() directly.

Use std::chrono::milliseconds as your elapsed_time -- why convert to seconds needlessly?

void Run(std::chrono::milliseconds ms)
{
  std::chrono::time_point<std::chrono::system_clock> start
    = std::chrono::system_clock::now();

  auto elapsed_time = [start]()->std::chrono::milliseconds
  {
    return std::chrono::system_clock::now() - start;
  };

  while (elapsed_time() <= ms) {
    std::cout << "Running" << std::endl;
  }
}
Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
0

Your problem is the data operation that you perform.

Have you try to print "elapsed_seconds"?

As explained in the documentation you have to do:

std::chrono::duration_cast<std::chrono::microseconds>(end - start).count()

By the way, what do you need could be done also with std::future:wait_for as suggested.

Luca Davanzo
  • 21,000
  • 15
  • 120
  • 146