chrono

Hi....so how to write this program with chrono...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <ctime>
#include <iostream>

int main()
{
    time_t cooldown;
    time_t holder;
    cooldown = clock();
    holder = cooldown + 200;
    while (cooldown <= 3000)
    {
        if (cooldown >= holder)
        {
            std::cout << "lol " << cooldown << std::endl;
            cooldown = clock();
            holder = cooldown + 100;
        }
        cooldown = clock();
    }
}


the main concept is there will be output every 100 millisecond
You probably want to check out this link. There's more than one kind of timer.
http://en.cppreference.com/w/cpp/chrono
I checked it out....but I dont really understand...
You can't write this program (perform output periodically, after some amount of used processor time) using the facilities in <chrono>.

std::clock measures the processor time used by the program.

The clocks in <chrono> measure elapsed wall clock time.

To perform output periodically, after 100 milliseconds of real time have elapsed:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <chrono>
#include <thread>

int main()
{
    const int num_intervals = 30 ;
    const int period_in_millisecs = 100 ;
    const auto period = std::chrono::milliseconds(period_in_millisecs) ;

    for( int i = 0 ; i < num_intervals ; ++i )
    {
        std::cout << "hello\n" << std::flush ;
        std::this_thread::sleep_for(period) ; // avoid busy idle
    }
}
well...I dont think I want the thread to sleep....so is there a way to get how many times has passed like

sf::Time time = clock.restart();
sf::Time elapsed = time;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <chrono>

struct timer
{
    using clock_type = std::chrono::steady_clock ;

    unsigned long long millisecs_elapsed() const
    {
        using namespace std::chrono ;
        const auto now = clock_type::now() ;
        return duration_cast<milliseconds>(now-start).count() ;
    }

    void reset() { start = clock_type::now() ; }

    clock_type::time_point start = clock_type::now() ;
};

int main() // usage example
{
    timer t ;
    char c ;
    while( std::cout << "? " && std::cin >> c )
    {
        std::cout << "that took " << t.millisecs_elapsed() << " millisecs\n" ;
        t.reset() ;
    }
}
Last edited on
so uhh...is it okay if I use class instead of struct ?
it's cool...but when I run this code using that class / struct

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
{
    timer t;
    for (int i = 0; i < 30; ++i)
    {
        while(1)
        {
            int time = t.millisecs_elapsed();
            if (time == 100)
            {
                std::cout << "lol " << time << " " << i <<std::endl;
                t.reset();
                break;
            }
        }
    }
}


it always reached less than 10
and one time it finished the code
I tried to run it without the for loop and only infinite loop..
it stopped...at random number of loop
Topic archived. No new replies allowed.