I have a 12 hour clock and a 24 hour clock. I need to have a thread that sleeps one second then calls the method to update by a second. I first tried the getc and unget c functions but have been told that I need to thread sleep for one second and then call method to update it by a second. Below is the snippet of code. Everytime I give user input to add an hour, or minute, and seconds to the clocks it also adds a second to the clocks at the same time. I don't think this is what is supposed to be happening. Any suggestions on how I can accomplish this task is appreciated. I have many tabs open adn the more I research the more confused I seem to get.
I don't understand what you're asking. Your program is adding to the time every second. And a user can also choose to update the time with their input?
But when the user types in input, you don't want it to add a second as usual? Or do you mean that user input TRIGGERS the addition of a second to time, even when it shouldn't?
std::this_thread::sleep_for is completely unsuitable as a timer.
See cppreference:
Blocks the execution of the current thread for at least the specified sleep_duration.
This function may block for longer than sleep_duration due to scheduling or resource contention delays.
The standard recommends that a steady clock is used to measure the duration. If an implementation uses a system clock instead, the wait time may also be sensitive to clock adjustments.
Note "Fixed pending release" is somewhat misleading because the fix would affect the Standard library's binary interface and therefore will not be released until a sane decision about binary compatibility is reached.
As mbozzi says above, sleep_for() will suspend execution of the thread for at least the specified duration. In practice, the suspension time could be longer - possibly much longer - depending upon other executing threads, priorities etc.
Windows, Linus etc are not real-time OS's so any timing is only approx.
Often if a suspension delay is appropriate (as opposed to using synchronization) then the exact actual suspension time isn't critical. However, when you want to update say a clock then it might well be. The longer the suspension, the more likely the exact suspension time is more than that specified. After some hours you could easily be a few seconds adrift.
In cases like this, I suggest that one approach is to obtain the new system time after execution has resumed.
As Pseudo:
1 2
Sleep(1s);
Obtain current time;
Another approach is (as pseudo):
1 2 3 4 5
Obtain the current time (t1);
Repeat {
Sleep(0); //which causes the thread to relinquish the remainder of its time slice to any other thread that is ready to run
Obtain the current time (t2);
} Until t2 - t1 is that required.
That's not how it would usually be coded. Use mod and int division
That is the best way, I put that in only for completeness - I had some code and I just switched out the "if"s to "while". int main() is what I believe is the relevant code that would help them.
They seem to not have differentiated between adding a second to the timer and adding the user input to the timer.