If you want to call a specific function from an already running program, use Boost Date Time and Asio .
#include <boost/date_time/gregorian/gregorian_types.hpp> #include <boost/date_time/posix_time/posix_time_types.hpp> #include <boost/asio.hpp> namespace gregorian = boost::gregorian; namespace posix_time = boost::posix_time; namespace asio = boost::asio; asio::io_service io_service_; asio::deadline_timer timer_ (io_service_);
Then set the time. This is an example using the desired time string, for example "18:00:00". (Note that Boost defaults to UTC for timestamps!)
gregorian::date today = gregorian::day_clock::local_day(); posix_time::time_duration usertime = posix_time::duration_from_string(time) posix_time::ptime expirationtime (today, usertime);
Now set the timer to foo() :
timer_.expires_at (expirationtime); timer_.async_wait (foo); // use Boost::bind() if you want a member function
You can add as many timers as you want timer_ using the above two code snippets. When everything is ready:
io_service_.run ();
Note that this will take the current thread. Therefore, you must call run() from another thread if you want the rest of your program to keep going. (Obviously, use the Boost Thread library for this.)
source share