• Welcome to the Speedsolving.com, home of the web's largest puzzle community!
    You are currently viewing our forum as a guest which gives you limited access to join discussions and access our other features.

    Registration is fast, simple and absolutely free so please, join our community of 40,000+ people from around the world today!

    If you are already a member, simply login to hide this message and begin participating in the community!

C++ help?

Tyson

Member
Joined
Apr 18, 2008
Messages
640
Location
Burlingame, CA, USA
WCA
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.
 

Lucas Garron

Administrator
Joined
Jul 6, 2007
Messages
3,718
Location
California
WCA
2006GARR01
YouTube
Visit Channel
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:

Johannes91

Member
Joined
Mar 28, 2006
Messages
1,341
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.
 

Johannes91

Member
Joined
Mar 28, 2006
Messages
1,341
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.
 

Stefan

Member
Joined
May 7, 2006
Messages
7,280
WCA
2003POCH01
YouTube
Visit Channel
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:

Bryan

Premium Member
Joined
Oct 20, 2007
Messages
1,296
Location
Rochester, MN
WCA
2007LOGA01
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.
 

Litz

Member
Joined
Dec 14, 2009
Messages
216
Location
Portugal
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?
 

Tyson

Member
Joined
Apr 18, 2008
Messages
640
Location
Burlingame, CA, USA
WCA
2004MAOT02
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:
Top