Click here to Skip to main content
15,887,416 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am a beginner in C++. Let's say I have 4 functions (func1(), func2(), func3() & func4()). And let's say I have assigned time values to be triggered for each function as below,
 
func1() - after each 3 seconds
func2() - after each 6 seconds
func3() - after each 9 seconds
func4() - after each 12 seconds

Eg:
    * After 3 seconds from the starting time of the program - func1()
    * After 6 seconds from the starting time of the program - func1(), func2()
    * After 9 seconds from the starting time of the program - func1(), func3()
    * After 12 seconds from the starting time of the program - func1(), func2(), func4()


How can I do this? A massive thank for helping me!


What I have tried:

I tried using high_resolution_clock
Posted
Updated 28-Jan-20 4:40am
Comments
ZurdoDev 28-Jan-20 8:27am    
There are tons of examples if you google c++ timer.
CPallini 28-Jan-20 11:07am    
Indeed.
Rick York 28-Jan-20 11:10am    
A precise answer depends on the development and working environment of your program. Are you using W32, QT, Wx, CLI, ...? The list goes on.

1 solution

The simple way would be something like this
C++
#include <chrono>
#include <thread>

int main()
{
   // some processing ....
   
   size_t seconds = 0;
   while(seconds < 60)
   {
        std::this_thread::sleep_for(std::chrono::seconds(1));
        seconds += 1;
        if( seconds % 3 == 0 )
             func1();
        if( seconds % 6 == 0 )
             func 2();
        // etc
    }

    // more processing
}


The problem with this approach is that your main() loop stops all work for 1 second, so if there's other things you want to be doing as well, then you're blocked until the sleep_for finishes.

If you try this within a loop and measure duration, you've got a couple of problems. Firstly, you'll almost never have an exact number of seconds, so you'll have to find a way to make sure you only trigger once per interval, and secondly, unless you've got other processing going on that needs to be done, then you're spending a lot of CPU time doing an "idle loop", which would drag down your system performace, and is probably something that you want to avoid.

Another, more advanced solution would be to put each function into its own thread with their own timing loop. That's tricky though: since each thread is triggering its own event, the order of execution will be undetermined, which may or may not be an issue. You will also have the problem of any shared resources (e.g. files, screen I/O, data blocks), which will need to be managed so that any changes get made in a sane manner. You will also need to be able to signal to your child threads to shutdown and clean up when the main thread ends.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900