How to Implement a Timer in C++ for Repeating Code Blocks

  • Context: Comp Sci 
  • Thread starter Thread starter jaydnul
  • Start date Start date
  • Tags Tags
    C++ Timer
Click For Summary

Discussion Overview

The discussion revolves around implementing a timer in C++ to manage the execution of multiple code blocks that need to run for a specified duration, specifically for blinking LEDs. Participants explore various approaches to ensure that each code block runs for 10 seconds before moving on to the next one, addressing issues related to looping and timing mechanisms.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant suggests restructuring the code to use a main loop that iterates through each chunk sequentially, questioning if the subsequent chunks depend on the completion of the first.
  • Another participant emphasizes the need for each chunk to run multiple times, specifically for blinking LEDs, indicating that timing is crucial for the visual effect.
  • A participant raises a question about how the program determines the duration of 10 seconds and suggests that the operating system may provide APIs that could be more effective than a manual timing loop.
  • One suggestion is to use a sleep function to pause execution between setting different LED states, allowing for a timed sequence.
  • A later reply proposes a code structure using a loop index and a timing mechanism based on system time, warning about potential overhead from the timing function and suggesting the use of C++11 features like the chrono library.
  • There is a mention that using a sleep function may not suffice for repeating actions over a specified duration without multithreading, indicating uncertainty about the effectiveness of this approach.

Areas of Agreement / Disagreement

Participants express differing views on the best approach to implement the timer functionality, with no consensus reached on a single solution. Various methods are proposed, each with its own assumptions and potential limitations.

Contextual Notes

Some limitations include the dependence on the operating system's capabilities for timing functions, the potential overhead of timing mechanisms, and the need for multithreading for certain implementations.

jaydnul
Messages
558
Reaction score
15
Basically within my whole code is 4 major chunks of code that each loop repeatedly. The obvious problem is when I run it, it will get stuck on the first chunk and loop forever, therefore never moving on the next chunk. What I need is to have each section run for 10 seconds and then move on. Is there a simple function that could do this?

Thanks a bunch!
 
Physics news on Phys.org
why can't you restructure your code with a main loop so that you do one iteration for each of the four chunks?

or does the subsequent chunks depend on the first chunk to be completed?

As an example

Code:
for(int i=0; i<max; i++) {
   dochunk1(i)
   dochunk2(i)
    ...
}
 
I need more than one iteration from each chunk at a time. They are blinking LEDs and need to be blinking for a certain amount of time before moving on to the next LEDs (simplified but you get the idea).
 
How is your program determining how long 10 sec. is? What OS are you using? The reason I ask is that the OS might have some APIs that you can use to do this, which would probably be better than you having an empty loop run for a set amount of time that you determine by trial and error.
 
look for a sleep function

so you set your LEDs then sleep then set them differently then sleep ...
 
Hmm. Maybe not what people suggested. Would do the following (and maybe this is what you want):
Code:
unsigned char cur_loop = 0; // an index.
unsigned char loop_count = 4; // you have four loops.
long interval = 10000 // 10000, assuming milliseconds, stand for 10 seconds per loop.
long time = current_system_time(); // use whatever type best suits your needs (or the underlying implementation).
while (true) {
  if (current_system_time() - time > interval) {
    cur_loop = (cur_loop + 1) % loop_count;
  }
  switch (cur_loop) {
     // cases and their blocks here (the magic happens here!)
  }
}
// Be warned of the overhead of current_system_time()!
Copying and pasting this will not do much. I suggest C++11 and the chrono library. Read the documentation. Good luck.
If you need to repeat for n seconds then go ahead a sleep function won't work without multithreading (noob here, so I may be wrong).
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 11 ·
Replies
11
Views
4K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
Replies
1
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
8K