Repeat loop every 'x' ms [duplicate] - c++

This question already has answers here:
Loop every 10 second
(7 answers)
Closed 7 years ago.
I'm trying to write a loop that runs once every x milliseconds, is there a way to do something like this effectively in c++?
Hope someone could help me with an example how I would go about writing a loop like this

One and the simplest approach would be using windows.h library's Sleep() function.
#include "windows.h"
...
while(1)
{
for(...) {} // your loop
Sleep(miliseconds);
if(something) { break; } // to prevent infinite looping.
}
...
A better solution would be using std::this_thread::sleep_for() from < thread > header.
more documentation and examples here.

Related

Is it a race condition? [duplicate]

This question already has answers here:
Race condition when incrementing and decrementing global variable in C++
(3 answers)
Closed 5 months ago.
Two threads are executing a function named job, inside which they are incrementing a global variable. Will there be a race condition here?
int i = 0;
void *job(void *args)
{
i += 1;
}
Yes, this might result in parallel access to the variable, which is a problem. To avoid that, it's recommended to declare i as std::atomic<int>.

How to make a count down in C++ [duplicate]

This question already has answers here:
Sleep for milliseconds
(20 answers)
Closed 1 year ago.
In python the time.sleep() is for count down time before execute next code:
import time
time.sleep(5)
print("something")
So what is the equivalent to that in C++. Sorry for asking this simple question but I am new to C++
The "sleep_for" allows you to give the time you want to delay, and "sleep_until" allows you to delay until a exact time.
#include
#include
int main() {
using namespace std::this_thread; // sleep_for, sleep_until
using namespace std::chrono; // nanoseconds, system_clock, seconds
sleep_for(nanoseconds(10));
sleep_until(system_clock::now() + seconds(1));
}

Can someone please explain why the output of this code is 3 and not 4? [duplicate]

This question already has answers here:
Unexpected Result in Macro
(2 answers)
Closed 2 years ago.
Can someone please explain why the output of this code is 3 and not 4?
#define square(x) (x*x)
int main () {
int x,y=1;
x=square(y+1);
printf("%d\n",x);
return 0;
}
The reason is that what preprocessor does about macros is quite like a search-replace. So you get y+1*y+1 which gives three. To avoid such problems
wrap every variable in macro definition with parentheses #define square(x) ((x)*(x))
use functions instead (prefered as less likely to run into random errors)

do{}while(0) vs an empty statement [duplicate]

This question already has answers here:
do { ... } while (0) — what is it good for? [duplicate]
(5 answers)
Why use apparently meaningless do-while and if-else statements in macros?
(9 answers)
Closed 4 years ago.
Trying to make a logging system that only logs data when a certain macro is defined, I've done something like this:
#ifdef _DEBUG
#define foo(a) std::cout << a << std::endl
#else
#define foo(a)
#endif
int main()
{
foo("Hello!");
return 0;
}
The function main, after pre-processing, expands to:
int main()
{
;
return 0;
}
However, on some places, I saw that people use do{}while(0) instead of an empty macro. I suppose that a compiler would optimize away both of these but I'm wondering is there an advantage that one has over another?
I am aware of the need for both an empty statement and do{}while(0) but what I do not know is the difference between the two.
I don't believe my question was fully read and compared to the ones that have been provided when marking as duplicate.

How to close a program while not in main [duplicate]

This question already has answers here:
How do I make a C++ console program exit?
(13 answers)
How can I immediately close a program in C?
(4 answers)
Closed 9 years ago.
Is there a line in c++ that allows me to terminate my program while I am not in main()?
For example, a code where if you die you have to quit the game? I don't want it to rely on the "trust" system.
Is there a line in c++ that allows me to terminate my program while I am not in main()?
I would not use the word "line" here - better say "function". For normal termination, you can use std::exit() (also see § 18.5/8 of the C++11 Standard):
#include <cstdlib>
void foo()
{
std::exit(EXIT_SUCCESS);
}
int main()
{
foo();
}
Here is a live example.