C ++ Setting while loop speed per second

I am relatively new to C ++, so I don't have much experience. I have learned Python and I am trying to create an improved version of the Python code that I wrote in C ++. However, I want it to work in real time, so I need to set the speed of the While loop. I am sure there is an answer, but I could not find it. I need a comparable code:

rate(timeModifier * (1/dt)) 

This was the code I used in Python. I can set the dt variable to make the calculations more accurate, and timeModifier to double or triple the speed (1 sets it in real time). This means that the program will cycle 1 / dt once per second. I understand that I can include time.h in the header, but I think I'm too new to C ++ to figure out how to pass this into my needs.

+4
source share
6 answers

You cannot do this in C ++. You need to manually call some sleep function in the loop, Sleep on Windows or usleep on * NIX.

+3
source

You can write your own timer class:

 #include <ctime> class Timer { private: unsigned long startTime; public: void start() { startTime = clock(); } unsigned long elapsedTime() { return ((unsigned long) clock() - startTime) / CLOCKS_PER_SEC; } bool isTimeout(unsigned long seconds) { return seconds >= elapsedTime(); } }; int main() { unsigned long dt = 10; //in seconds Timer t; t.start(); while(true) { if(t.elapsedTime() < dt) { //do something to pass time as a busy-wait or sleep } else { //do something else t = Timer(); //reset the timer } } } 

Please note that waiting for a wait is not recommended, as they will process the CPU. If you do not need to do anything, use the sleep (Windows) or usleep (Linux) command. For more information on creating C ++ timers, see this link .

+5
source

Some time has passed since I did something similar, but something like this will work:

 #include <time.h> time_t t2, t1 = time(NULL); while(CONDITIONS) { time_t t2 = time(NULL); if(difftime(t2, t1) > timeModifier) { //DO the stuff! t1 = time(NULL); } } 

I should note, however, that I am not familiar with the accuracy of this method, I think it measures the difference in seconds.

If you need something more precise, use the clock() function, which has a number of milliseconds from 12:00 AM from January 1, 1980 to the next 10 milliseconds. Maybe something like this:

 #include <time.h> clock_t t2, t1 = clock(); while(CONDITIONS) { t2 = clock(); if((t2-t1) > someTimeElapsed*timeModifier) { //DO the stuff! t1 = clock()); } } 

Update: You can even cast the processor to other threads and processes by adding this after the if ends:

 else { usleep(10000); //sleep for ten milliseconds (chosen because of precision on clock()) } 
+2
source

Depending on the required accuracy and your platform, you can use usleep This allows you to set the pause time to microseconds:

  #include <unistd.h> int usleep(useconds_t useconds); 

Remember that your cycle will always take longer than due to the inherent processing time of the rest of the cycle, but this is the beginning. For something more precise, you may have to look for timer-based callbacks.

0
source

You really have to create a new thread and synchronize so that it remains unaffected by the processing performed in the loop.

WARNING: Pseudo code ... just to give you an idea of ​​how to get started.

 Thread* tThread = CreateTimerThread(1000); tThread->run(); while( conditionNotMet() ) { tThread->waitForTimer(); doWork(); } 

CreateTimerThread() should return the thread object that you want, and it will run something like:

 run() { while( false == shutdownLatch() ) { Sleep( timeout ); pulseTimerEvent(); } } waitForTimer() { WaitForSingleObject( m_handle ); return; } 
0
source

On Windows, you can use the QueryPerformanceCounter to query time (for example, in another while loop) to call Sleep(0) to allow other threads to continue operations.

Remember Sleep very inaccurate. For complete control, simply run the loop without operations, however you will use 100% CPU. To relax the load on the CPU, you can call Sleep(10) , etc.

0
source

Source: https://habr.com/ru/post/1413162/


All Articles