0

How can I manage my code to exactly wait for a certain amount of time (say 10s), if all * time functions on the system are not reliable and return immediately ?

Sleep(10000); // Do not sleep for 10s and execute directly the next function
MyFunction();

The solution should if possible, consume the fewer possible CPU cycles.

Of course they are no internet connection available

*WaitForSingleObject, GetSystemTime, RDTSC and so on...

Jo Doe
  • 23
  • 3
  • 1
    So you want to monitor time without having time ? – Christophe Sep 28 '16 at 19:38
  • I have time but time functions in this specific system are not reliable in the sense that time appear to elapse more rapidly than in the real world – Jo Doe Sep 28 '16 at 20:01
  • Can you explain in more detail what you mean by "all time functions are not reliable?" – Robert Harvey Sep 28 '16 at 20:02
  • @RobertHarvey In the sense that the time seem to elapse more rapidly, for example two delta between one function call will appear to be less than on an usual machine, all waitable timer will trigger directly... – Jo Doe Sep 28 '16 at 20:09
  • What will you use as a baseline time standard? – Robert Harvey Sep 28 '16 at 20:09
  • @RobertHarvey I don't understand your question ? The second ? A counter ? – Jo Doe Sep 28 '16 at 20:19
  • You've tagged this for Windows and you've listed a handful of Windows system calls but you haven't mentioned the `SleepEx` system call which seems to be what you're after. Have you used that to sleep for a period of time? Of course, that won't be exact. If you need more precision than Windows can offer then you're in the realm of building a real-time system which would require a real-time operating system. – Justin Cave Sep 28 '16 at 20:19
  • @RobertHarvey A baseline time standard universal between all turing machine in the universe if possible :D. JustinCave No matter which API I call the time past faster than in any other real computer, the real problem is not precision offered by the OS but the fact that the OS deliberately manipulate all time functions to past faster – Jo Doe Sep 28 '16 at 20:50
  • @JoeDoe how come ? Some distortion in the time-space ? Could you use this PC to predict the future ? Joke asside, I really don't understand this problem and I am sorry for that: why not reinstall the OS on this computer to have a normal and predicatble situation ? There's not no OS independent way to properly handle that, because software interactions might be distorted depending on power setting and simultaneous threads, and OS controls the timing devices... – Christophe Sep 28 '16 at 21:13
  • If what you really want is a system that won't progressively perform worse as more and more timed events are requested I can show you how to design that. – candied_orange Sep 28 '16 at 22:51
  • Are you saying the hardware clock on your computer is unreliable? This can happen due to a bad motherboard or a low CMOS battery. It's a hardware problem and you really can't work around it in software without an internet connection to continually re-sync your clock. Try replacing the CMOS battery. If that doesn't work you may need to replace the computer. – Charles E. Grant Sep 29 '16 at 01:13
  • 1
    @CharlesE.Grant more likely he's noticed that in a multithreaded environment or even merely on an architecture with interrupt context switching you can't reliably predict how much time will pass going from one instruction to the next. Or he's noticed that polling the clock can have significant drawbacks.. – candied_orange Sep 29 '16 at 02:15
  • @RobertHarvey still waiting for the answer of THE man who have written software for the NASA – Jo Doe Sep 29 '16 at 16:30
  • I don't understand your question. Your question appears to be "how can I detect time unreliability without comparing it to a reliable time reference?" You can't. – Robert Harvey Sep 29 '16 at 16:33
  • @RobertHarvey I don't know, it exactly more or less the question that I'm asking, trying to break this paradox, for your question I can reply with that : if you are making another thread and calling Sleep(INFINITE); and the call return you can be more or less confident that something is going wrong. I don't have all the imagination, philosophical and physics knowledge in the world in my little brain it's why I'm asking. The computer in a certain way want to predict the future (at least for the behaviors of my program)... – Jo Doe Sep 29 '16 at 17:03
  • Have you perhaps checked the documentation for the function you're calling to find out whether there are conditions that would cause it to return before the time you asked for has elapsed and how it might tell you that? – Blrfl Sep 29 '16 at 17:51
  • @JoeDoe: the problem is the question you think you are asking isn't what you actually asked, and what you asked doesn't make any sense. We're not mind readers. – whatsisname Sep 29 '16 at 19:06

2 Answers2

1

It's not possible. If you don't have access to a working API for keeping track of time, then you cannot keep track of time.

Your best bet is probably to write a "useless" loop that does nothing but waste time, and then figure out how many times that loop needs to run in order to delay the program by the amount that you need. This will, of course, consume a lot of CPU cycles.

You might be able to avoid consuming so many CPU cycles by performing some useless activity which is I/O-bound, like repeatedly opening and closing a file.

Tanner Swett
  • 1,359
  • 8
  • 15
  • 3
    Even a busy loop is likely to be unreliable. Different computers will run it at different speeds and even the same computer may vary the time if it boosts or throttles. In some cases, a compiler may realize that the loop doesn't appear to be doing anything and optimize it out entirely. – 8bittree Sep 29 '16 at 14:32
  • It is more or less what I have thought however yeah a lots of CPU cycles being consumed, thanks for your last idea :) https://crypto.stackexchange.com/questions/606/time-capsule-cryptography – Jo Doe Sep 30 '16 at 05:44
1

I think this is what you are after:

using System.Threading.Tasks;

var task = Task.Run(() => MyFunction());
if (task.Wait(TimeSpan.FromSeconds(10)))
    return task.Result;
else
    throw new Exception("MyFunction did not complete in time.");

Since you already attempt to Sleep() in your example code, I assume that the computer's timer is reliable enough to make this work.

Further Reading
Crafting a Task.TimeoutAfter Method

Robert Harvey
  • 198,589
  • 55
  • 464
  • 673