Is there any built-in functionality in c++ to run task at scheduled time? (Some thing like cron?) Or have I to implement it myself?
Asked
Active
Viewed 110 times
1
-
1Using `this_thread::sleep_for` and `system_clock::now` it is possible to implement in a few lines. – erenon Mar 20 '16 at 11:27
-
Define "task". Are you talking about a process or a thread? – Nicol Bolas Mar 20 '16 at 14:03
-
http://stackoverflow.com/a/14665230/1870760 might come in useful. – Hatted Rooster Mar 20 '16 at 14:06
1 Answers
0
The closest you can do is to wait on a mutex or other synchonization primitive for a specified period of time.
"Release" the mutex (or fire a condition variable, etc) if the app is told to shut down (always a good plan), and if it times out, otherwise run the task.
Note that there are no realtime guarantees -- wait in C++ waits "at least that long". Basically, if the system is busy, tough.
This can be done in a thread, naturally, so whatever main functionality (message pump or whatever) isn't bothered by it.
You can also sleep for a specified period of time, but programs should always have an abort possibility, and sleeping doesn't provide it.

Yakk - Adam Nevraumont
- 262,606
- 27
- 330
- 524