   Coding an "every" function in C++
  How would I go about coding a function in C++ that calls another every, however many seconds is passed in?Thanks.   
  I think you'll find what you're looking for in this thread:c++ timer;Quote:Original post by 3DModelerManHow would I go about coding a function in C++ that calls another every, however many seconds is passed in?Thanks.You could write a simple function with a loop, which calls the function, sleeps a bit, and repeats. Before going to sleep, you will ideally put your process into low-priority, so that upon idling your application doesn't eat too much (under linux there is a convenience function that does a kind of low-priority-sleep):#include <ctime>using std::clock;using std::clock_t;template <typename T> void callEveryN (float tf, T fun) {   const clock_t t = static_cast<clock_t> (tf * static_cast<float>(CLOCKS_PER_SEC));   while(1) { fun (); const clock_t lastT = clock(); while (clock() - t < lastT)    ;   }}Above code is standards compliant. You can use it on those, for instance:#include <iostream>void printHelloWorld () {   std::cout << "Hello, World" << std::endl;}class Foo {public:   void operator () () const { std::cout << "Foo::operator ()" << std::endl;   }};and then use you callEveryN() like this:int main () {   callEveryN (1.0f, printHelloWorld);   Foo foo;   callEveryN (1.0f, foo);}But note that callEveryN() does not halt because of its infinite loop.Or are you really looking for how to implement a timer-function like found in several libraries, i.e. of the thread- or event-based sort?   ;Quote:How would I go about coding a function in C++ that calls another every, however many seconds is passed in?Thanks.Can you be more specific? In particular:1. Do you already have a means of keeping track of elapsed time (e.g. a timer class, a 'get ticks' function, or something similar)?2. Are you wanting this to happen in its own thread?3. Can you give an example of what the function would do, and/or tell us why it needs to be called at specific intervals?[Edit: A little slow...]   ;  @OP: Kuckuck, any progress so far?   ;  I want it to be in the same thread. And yes I have a timer class. Sorry it took so long to respond. I lost the topic.   ;  Near the top of the thread, there is a button labeled "Track this topic". Once clicked, it'll add it to your bookmarks, so you can easily find it again.   ;Quote:Original post by 3DModelerManI want it to be in the same thread. And yes I have a timer class. Then the solution I gave to you should does exactly that job. But as noted it can't halt, and as you wanted to have a special function with only one master-thread, it seems like the only solution.Alternatively, for you haven't specified your idea more precisely, something like that works, too:main() {    clock_t lastT = clock()    program-loop {   clock_t currentTime = clock()   if (currentTime > lastT) {  lastT = currentT  doTimeyThings()   }    }}Of course you could decorate above time-thing in a functor.Quote:Sorry it took so long to respond. I lost the topic.No problem. Simply note that appreciation is the only valuta here ;);  Okay, thanks I get it now.   