This question already has answers here:
How to use a timer in C++ to force input within a given time?
(5 answers)
Closed 6 years ago.
I'm writing a code where the user has to answer the question in 10 seconds or else he will lose, I know that I'm supposed to use threads but the problem is, I don't know how to do the timer, any ideas?
Thanks
You don't need threads.
Assuming time is in ms:
updateLoop(){
timeLeft = startingTime + 10.000 - now;
if (timeLeft <= 0) {
lose=true;
}
render();
}
EDIT: I probably misunderstood the question a bit. This seems to cover your question, as in #Mourad's comment:
How to use a timer in C++ to force input within a given time?
Related
This question already has answers here:
How to determine CPU and memory consumption from inside a process
(10 answers)
Closed 7 years ago.
I have a few threads in my program - run on Windows and write in C++.
How can I know in the end of the running how much CPU time is used by all or one of them?
You can use the GetThreadTimes function: https://msdn.microsoft.com/en-us/library/windows/desktop/ms683237%28v=vs.85%29.aspx
This question already has answers here:
How to Calculate Execution Time of a Code Snippet in C++
(18 answers)
Closed 8 years ago.
I'd like to ask, which method of measuring the time which is required for function to execute is considered the best?
Just curious.
Thanks.
You can try to use rdtsc(); it's accurate, perhaps fastest and cross-platform (but only for x86 architecture). For example, in Windows:
#include <intrin.h>
uint64_t rdtsc()
{
return __rdtsc();
}
This question already has answers here:
How to measure the inner kernel time in NVIDIA CUDA?
(2 answers)
Closed 8 years ago.
I searched a bit but all things I found could only be annotated in CPU code, how could I measure partial time inside kernel between 2 _syncthread() of 1 threadblock? Is it possible?
One approach is to use the clock() or clock64 function as described in the programming guide.
Search the cuda tag on clock64 for additional examples of its usage.
This question already has answers here:
How to get memory usage under Windows in C++
(5 answers)
Closed 9 years ago.
Under windows, given a PID:
1) How to get the exact memory in bytes and
2) The exact CPU usage
consumed by that application right now?
See GetProcessMemoryInfo, and specifically the WorkingSetSize field of the out parameter.
GetProcessTimes will let you know how much time your process has spent altogether in user & kernel space. It's up to you to calculate percentages, or whatever you want out of it.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Sleep Less Than One Millisecond
Is there a way to "sleep" for less than a millisecond in C++, but without a busy loop?
No. Under Windows, even without sleeping you can't ensure that two consecutive instructions will be carried out with less than a millisecond between them.