3

I need to make something(i call it a scheduler) that checks the time of the sytem every minute and if the time has changed suppose it is 17:52 and the next moment it is 17:53so at 17:53 it calls a function logupdated

How do i make this simply i m not known to the mutex and all.

Thanks

gandhigcpp
  • 587
  • 3
  • 8
  • 16
  • 3
    First, decide: do you want to call a function every 1 minute or at every minute clock tick of your system time? These are different functions. – KillianDS Apr 09 '12 at 12:28
  • What platform? On Unix use `cron`. – David-SkyMesh Apr 09 '12 at 12:29
  • Do you expect this program to run in the background of your system all the time? If you do, you may want to look at cron-jobs on unix, or writing a windows service with the .net framework on windows. Does the application do any work between calls to the logger? Are you writing a multi threaded process? – Darcy Rayner Apr 09 '12 at 12:30
  • @KillianDS:considering it runs forever i would say every clock tick also i have got some functionality for the roatting logs as well – gandhigcpp Apr 09 '12 at 12:31
  • @David-SkyMesh:on windows 7 home premium 64 bit – gandhigcpp Apr 09 '12 at 12:33
  • @gandhigcpp: http://stackoverflow.com/questions/132971/what-is-the-windows-version-of-cron – David-SkyMesh Apr 09 '12 at 12:35

2 Answers2

7

I am not sure I understand the requirements, but your question reads "how to execute a particular code in c++ after every 1 minute", so, in c++11 you can do this:

#include <thread>
#include <chrono>

int main() {

  while (true) {
    std::this_thread::sleep_for(std::chrono::seconds(60));
    // call your c++ code
  }

}
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • @KillianDS good point. I hadn't considered the duration of the task itself. I guess if there is any possibility of overlap, it should also run in a separate thread or process. – juanchopanza Apr 09 '12 at 13:00
  • :dont know what is c++ 11 may be an upgraded version however i have only got ansi c++ here with me and i m not sure if netbeans ide 7.1.1 has a support for it – gandhigcpp Apr 09 '12 at 13:01
  • @gandhigcpp c++11 is the new standard, so it ***is*** c++. However, there is little compiler support at the moment. GCC covers the code snippet I included, but look at @KillianDS's answer concerning `boost` alternatives. – juanchopanza Apr 09 '12 at 13:06
  • :there is an error at the statement std::this_thread<-at "this_thread" – gandhigcpp Apr 10 '12 at 05:49
  • :do i need to declare "this_thread" somewhere it gives me an error while testing it – gandhigcpp Apr 10 '12 at 05:52
  • @gandhigcpp `this_thread` is in the ``header. If the code snippet above doesn't compile as is, it could be that your compiler doesn't support this yet. – juanchopanza Apr 10 '12 at 05:58
  • :yes it does not compile as it it is what is the other way round , one thing more guys please dont take me wrong here i am a novice so i require a lil bit more help from u people – gandhigcpp Apr 10 '12 at 06:52
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/9906/discussion-between-gandhigcpp-and-juanchopanza) – gandhigcpp Apr 10 '12 at 10:56
3

If you want the execution of the task to be independent of the main program flow, consider multithreading.

this example in C, should work also on C++ Note that some people think that I use pointers excessively, I agree, specially with multithreading, it can cause unsafe threads, thus data corruption or even worse, segmentation faults. However this is the only way to pass arguments to threads as far as I could find.

#include <pthread.h>
int main(int argc, char *argv[]) {
  pthread_t thread1;
  int variables=10;
  pthread_create( &thread1, NULL, scheduler, (void*)&variables);
  while(1){
    .... do stuff as main program.
  }
  return 0;
}

void *scheduler (void* variables) {
  int vars;
  int* p_vars = (int*) variables;
  vars = *p_vars;
  while (1){
    .. do scheduler stuff
    sleep (vars);
  }
}
Aus
  • 1,183
  • 11
  • 27