I need simple timer that should not mess with the threads,and doesnt use Sleep().

I need something like this:

int main(){

function1 {
          
          whule(1) {
                   // do stuff
                   execute evry 30 min function2();
                   }
          }
          return 0;
}

So function1 and while(1) must not be slowed down and need to run all the time.

And success of function2() must not affect running the function1.

How to do this?

Recommended Answers

All 15 Replies

Even if you call the system time to do it every 30 minutes
requires a trigger device and as such requires some method
of checking when the time changes.

If you have an external program that sends a message this will still
in effect be slowing down the running of the function.

What is it that you are doing that is running true and wants to do
something every 30 minutes ?
what are you trying to do with function2() ?

You can add a counter in ints with only a minimal overhead

//in the while
if(--counter < 0)
{
//get system time if elapsed time > 30 mins start function2();
counter = counter_start;
}

I need simple timer that should not mess with the threads,and doesnt use Sleep().


And success of function2() must not affect running the function1.

Needs more explanation of function2() to know whether it should be function2() doing the timing.

If you want 30 minutes:
Call time() to get the current time - say tstart
Add number of seconds in 30 minutes to tstart.
Call time() every cycle -- say tnow
When the tnow >= tstart, call function2.
When function2 returns, reset tstart

hm

Call time() every cycle -- say tnow

but this is not going to decrement -1 each second,it will decrement it very fast with cpu clock :S ???

Read up on the function. Don't make invalid assumptions.

time_t tstart=time(0); 
    time_t tnow=tstart+30*60;
    tnow--;
    if(tnow>=tstart)
    {
        f2(){tsart=time(0)};
    }

I cant find that time() func in google?

#include <cstdlib>
#include <iostream>
#include <ctime>



using namespace std;

int main(int argc, char *argv[])
{
    
    cout << time(0)<< endl;
    
    
    time_t tstart=time(0); 
    tstart=tstart+5;// xMinuta*60
    time_t tnow;
    while(1){
    tnow=time(0);
    
    if(tnow>=tstart)
       {
        //f2(){tstart=time(0)};
        cout << "HELLO WORLD" << endl;
        tstart=time(0)+5;// xMinuta*60
        
       }
    }      
                     
    system("PAUSE");
    return EXIT_SUCCESS;
}

hm,man this works BUT IT USES 100 CPU :S :S :S

how to solve it?

Try to make your own using the ctime header. Here is some starter code

class Timer{
private:
  long startTime;
  long endTime;
public:
 Timer() {
   startTime = endTime = clock();
  }
 void startTimer(){
 }
 void stopTimer(){
 }
 long getElapsedMilliSeconds(){
 }
 long getElapsedSeconds(){
   return getElapsedMilliSeconds() * CLOCKS_PER_SEC;
 }
 float getElapsedMinutes(){
  return getElapsedSeconds() / 60.0f;
 }
};

obviously you have to fill in the blank code.

hm,man this works BUT IT USES 100 CPU :S :S :S

how to solve it?

use Sleep :icon_wink:

You said:

I need simple timer that should not mess with the threads,and doesnt use Sleep().

What you wrore doesn't do anything but spin on the timer so it'll use whatever CPU is available.

But if you add this to the thread you need the timer, you'll do work. Then when the timer hits your 30minute mark, the timer code will execute once (if you write it correctly). Then the thread will continue as you need.

use Sleep :icon_wink:

You said:

What you wrore doesn't do anything but spin on the timer so it'll use whatever CPU is available.

But if you add this to the thread you need the timer, you'll do work. Then when the timer hits your 30minute mark, the timer code will execute once (if you write it correctly). Then the thread will continue as you need.

thank you for help!

Try to make your own using the ctime header. Here is some starter code

class Timer{
private:
  long startTime;
  long endTime;
public:
 Timer() {
   startTime = endTime = clock();
  }
 void startTimer(){
 }
 void stopTimer(){
 }
 long getElapsedMilliSeconds(){
 }
 long getElapsedSeconds(){
   return getElapsedMilliSeconds() * CLOCKS_PER_SEC;
 }
 float getElapsedMinutes(){
  return getElapsedSeconds() / 60.0f;
 }
};

obviously you have to fill in the blank code.

you wrote .h file,and I should do .cpp,wright?

where to find more info about clock() and CLOCKS_PER_SEC?

#include <cstdlib>
#include <iostream>
#include <ctime>



using namespace std;

int main(int argc, char *argv[])
{
    
    cout << time(0)<< endl;
    
    
    time_t tstart=time(0); 
    tstart=tstart+5;// xMinuta*60
    time_t tnow;
    while(1){
    tnow=time(0);
    
    if(tnow>=tstart)
       {
        //f2(){tstart=time(0)};
        cout << "HELLO WORLD" << endl;
        tstart=time(0)+5;// xMinuta*60
        
       }
    }      
                     
    system("PAUSE");
    return EXIT_SUCCESS;
}

so is this method convenient for long time pauses like 2-3 days?

because tstart needs to be large number?

so is this method convenient for long time pauses like 2-3 days?
because tstart needs to be large number?

In terms of being sufficient, the time_t data type is OK. Check out how many bits your time_t actually is, (probably 64).

Then again, this is the same construct that you already tried and you stated; "man this works BUT IT USES 100 CPU", so will it be convenient to use ~100% CPU for a couple of days?

thread solved

thank you all!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.