How to have a function be called every second

Hey, I am working on this project where I need a function to be called every second. At this time, I am thinking that I have to create a thread but I am clueles on how it will get called every second.Can anyone point be in the right direction.
Thanks In Advance
closed account (ETAkoG1T)
Not sure exactly what you want but I'll give it a try:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <windows.h>
#include <iostream>

inline void HelloWorld()
{
      std::cout << "Hello World" << std::endl;
}

int main()
{
      while(1)
      {
          // Do other stuff here, assuming it doesn't take more than 0 sec...
         Sleep(1000);
         HelloWorld();
      }
      return 0;
}
Last edited on
Filiprei, this is going to stop the execution of code for 1 second.

Threads will be a good idea. just pass the function pointer to the thread and let thread call the function every 1 second.
closed account (ETAkoG1T)
I didn't know what he wanted, and if the exact time wasn't extremely important it could work fine.
Writetonsharma,thanks for your advice. Is it possible for you to provide an example of what you suggested?
Thanks again!!
With a thread, it could be something like

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <iomanip>
#include <thread>
#include <chrono>
#include <boost/date_time.hpp>
namespace bpt = boost::posix_time;
void f()
{
    std::cout << "Called at " << bpt::microsec_clock::local_time().time_of_day() << '\n';
}

void caller()
{
    for(int n=0; n < 5; ++n) {
        f();
        std::this_thread::sleep_for(std::chrono::seconds(1));
    }
}

int main()
{
    std::thread thr(caller);
    thr.join();
}


Called at 12:12:28.724024
Called at 12:12:29.724359
Called at 12:12:30.724550
Called at 12:12:31.724746
Called at 12:12:32.724910

(note the drift: it waits 1 whole second, but then it spends some extra time calling that function, so the next call is slightly more than one second later)


If you want to avoid the drift, you could use a boost timer:

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
#include <iostream>
#include <iomanip>
#include <boost/asio.hpp>
#include <boost/date_time.hpp>
#include <boost/bind.hpp>
namespace bpt = boost::posix_time;
namespace asio = boost::asio;
void f()
{
    std::cout << "Called at " << bpt::microsec_clock::local_time().time_of_day() << '\n';
}

void caller(const boost::system::error_code&, asio::deadline_timer& t, int& count)
{
    f();
    t.expires_at(t.expires_at() + bpt::seconds(1));
    if(++count < 5)
        t.async_wait(boost::bind(caller, asio::placeholders::error, boost::ref(t), boost::ref(count)));
}

int main()
{
    asio::io_service io;
    asio::deadline_timer t(io, bpt::seconds(1));
    int count = 0;
    t.async_wait(boost::bind(caller, asio::placeholders::error, boost::ref(t), boost::ref(count)));
    io.run();
}


Called at 12:22:34.469683
Called at 12:22:35.469681
Called at 12:22:36.469684
Called at 12:22:37.469685
Called at 12:22:38.469696
Last edited on
Topic archived. No new replies allowed.