Call a function over a period of time

I need to make a timer or something like that to call a function over and over again for like 20 seconds.. i cannot use windows.h im using xcode on mac.

1
2
3
4
5
6
7
8
// example  
if(input == "Cutting"){
   
      // timer here wich can call a function over a peroid of time
      // and call myfunc(); over and over again for 20 seconds
      // then after 20 seconds stop calling it and call function inputs(); once done 

  }
Last edited on
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <string>
#include <chrono>
#include <thread>

std::string get_input()
{
    std::string str ;
    std::cout << "? " ;
    std::cin >> str ;
    return str ;
}

void my_func()
{
    std::cout << "my_func\n" ;
    using namespace std::literals ;
    // http://en.cppreference.com/w/cpp/thread/sleep_for
    // http://en.cppreference.com/w/cpp/chrono/operator%22%22ms
    std::this_thread::sleep_for( 800ms ) ;
}

int main()
{
    std::string input ;
    do
    {
        input = get_input() ;
        if( input == "Cutting" )
        {
            using namespace std::chrono ;
            using namespace std::literals ;
            const auto start = steady_clock::now() ;
            do
                my_func() ;
            while( duration_cast<seconds>( steady_clock::now() - start ) < 20s ) ;
            // http://en.cppreference.com/w/cpp/chrono/steady_clock
            // http://en.cppreference.com/w/cpp/chrono/duration
            // http://en.cppreference.com/w/cpp/chrono/duration/duration_cast
            // http://en.cppreference.com/w/cpp/chrono/operator%22%22s


        }
    }
    while( input != "quit" ) ;
}
Topic archived. No new replies allowed.