C++ help?

Tyson

Veteran
Joined
Apr 18, 2008
Messages
640
Location
Burlingame, CA, USA
WCA ID
2004MAOT02
I'm trying to learn to code, but I really have no background. So I was hoping I could get some help.

Is there a way to write code in C++ such that you can get your code to perform a certain action at a certain time?

I'm currently doing this by using <time.h> and <sys/time.h> and there's a time(null) function that will return the current time. So the way I'm kind of doing it is like...

while( time(null) < someTime) {
do stuff
}

and then stuff happens at someTime.

But this requires my code to call the time(null) function over and over and over again, and I'd prefer not to do that. Is there functionality in C++ so that I could have something along the lines of:

At 3:30pm, std::cout << "Tyson sucks at coding" << std::endl;

And be able to do this without having my code keep asking what time it is.

Thanks in advance. If anyone is interested in the field of high-frequency trading, I'd be happy to have a conversation. There are always tons of opportunities for developers. Yeah, I'm not much of a coder so I can't really contribute there, but if any coders want to learn about trading, that's how I can reciprocate.
 
Does this need to be done IN the program? Otherwise, a cron job sounds right.

Anyhow, if the execution time is not accuracy-critical, you can have a for loop like yours that sleeps about a minute, and exits after the system time exceeds 3:30. (If you want to go extra smart, you can also have it loop less frequently, try to estimate the time left, then loop more frequently as the time approaches.) Not very expensive, and simple enough for some applications.

Is this multi-threaded?

EDIT: What OS?
 
Last edited:
Anyhow, if the execution time is not accuracy-critical, you can have a for loop like yours that sleeps about a minute, and exits after the system time exceeds 3:30.
Or just calculate how much time is left until stuff needs to happen and sleep that amount. No need for a loop.

If it's possible that a new event between this moment and 3:30 appears and other stuff needs to happen then, sleeping all the time wouldn't work, but it's hard to say much without knowing more context.
 
If that works for you, then it's fine, but it's possible to make the process really sleep so that it doesn't use any CPU time. This is desirable especially if it sleeps a long time and other processes could use the CPU instead.

The C++ standard library doesn't include a sleep function (AFAIK), so it depends on the OS and possibly the compiler you're using.
 
Like I said, 1-2 milliseconds accuracy is unlikely with this. Check out this test I just did, first checking my CLOCKS_PER_SEC (luckily, it's indeed milliseconds on my computer) and then recording all clock() value changes. As you can see, clock() does not report all possible values, it does jump in 15-16ms steps. Can you try this on your machine and post the output?

Code:
#include <iostream>
#include <ctime>
using namespace std;

clock_t stamps[100000];

int main ( int argc, char *argv[] ) {
  cout << CLOCKS_PER_SEC << endl;
  stamps[0] = clock();
  int i = 1;  
  while ( i < 100000 && (stamps[i] = clock()) - stamps[0] <= CLOCKS_PER_SEC ) {
    if ( stamps[i] > stamps[i-1] )
      i++;
  }
  for ( int j=0; j<i; j++ )
    cout << stamps[j] << (j%10-9 ? ' ' : '\n');
}
Code:
1000
46 62 78 93 109 125 140 156 171 187
203 218 234 250 265 281 296 312 328 343
359 375 390 406 421 437 453 468 484 500
515 531 546 562 578 593 609 625 640 656
671 687 703 718 734 750 765 781 796 812
828 843 859 875 890 906 921 937 953 968
984 1000 1015 1031 1046
 
Last edited:
Can you tell us what your overall goal is? Sometimes people get started down on the wrong path and ask for help on how to implement the wrong path.
 
Like I said, 1-2 milliseconds accuracy is unlikely with this. Check out this test I just did, first checking my CLOCKS_PER_SEC (luckily, it's indeed milliseconds on my computer) and then recording all clock() value changes. As you can see, clock() does not report all possible values, it does jump in 15-16ms steps. Can you try this on your machine and post the output?
That only works for all values on some computers. I tested this once on an old computer and it jumped some ms (I needed the precision) but as it was pretty old and running on Windows 98, so I used the RTC and interrupts to get the real value.

On this computer I'm at now though, it does show all possible values. He said he's just learning so he doesn't really need precision probably. May I ask what's your goal Tyson?
 
I'd be happy to talk to people in person about the ultimate goal, but I can't post it on the forum.

You know, stuff. Some software for my job. I'll research some of the things posted here and see if I can make improvements. Thanks for all your help so far! I'm such a n00b.
 
Last edited:
Back
Top