0

I'm using a busy loop to check when a condition is true and take a picture, but I need to have less than 10millisecond delay when the condition is true, and when my loop detects it. Is this loop viable?

This is running on a slow raspberry pi zero.

for (;;) {

  for (pin = 0; pin < 8; ++pin) {
    // Some other thread changed counter, gets detected here
    if (globalCounter[pin] != myCounter[pin]) {
      //Take picture
    }
  }
}

full code I'm looking to adopt: https://github.com/WiringPi/WiringPi/blob/master/examples/isr.c

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Lightsout
  • 3,454
  • 2
  • 36
  • 65
  • 4
    This should run in way less than 10 ms. But the only way you will know how fast it runs is profiling it/checking yourself. The answer will vary on architecture, code and various other factors. – Rietty Oct 21 '19 at 22:44
  • 10ms is max, <1ms would be ideal for me – Lightsout Oct 21 '19 at 22:46
  • 3
    BTW, You should also be careful when accessing a variable in different threads. Make sure you access them in a thread-safe manner (if you haven't done so already ) And mind cross-thread visibility issues. – ph3rin Oct 21 '19 at 22:51
  • 4
    Use a `std::condition_variable` and tell the thread when it's been updated instead of busy-looping. – super Oct 21 '19 at 22:55
  • 2
    Generally unless you need sub-microsecond latency, you shouldn't be using busy-wait. – Jason Oct 21 '19 at 23:04
  • I hope `globalCounter` is `std::atomic globalCounter[]` or something like that, otherwise this has undefined behaviour if another thread writes it. (Compilers will potentially hoist the loads out of the loop if `atomic` doesn't stop them) – Peter Cordes Oct 21 '19 at 23:36
  • @super: the `condition_variable` has no guarantees about latency, although working most of the time, it could sometimes wait longer. – JVApen Oct 22 '19 at 06:16

1 Answers1

4

Code latency, specially those that depend on hardware, should be tested in conjunction with the rest of the code and in the target environment.

I believe it is impossible to give a "yes/no" answer without actually running a test in the physical system, with everything implemented.

My only suggestion would be that busy loops are usually a waste of resources and hard to maintain and can often be avoided by triggering the desired behavior only when a hardware flag is set or when the target variable changes in code.

ianmandarini
  • 395
  • 1
  • 15
  • The code I'm looking at is from a popular library and it's using busy loop so not by my choice see link. – Lightsout Oct 21 '19 at 23:06
  • 2
    You could adapt it. Albuquerque is right; busy waiting is bad and you have no good reason to do it. Especially when resource constrained!! – Lightness Races in Orbit Oct 21 '19 at 23:09
  • 1
    Usually, API examples are not the best regarding efficiency and coding style. They are usually made to exemplify a functionality of the library and to simplify everything else. I believe the cost of making a busy wait is being "ignored" in the example for the purpose of not adding too much complexity to it. – ianmandarini Oct 21 '19 at 23:27
  • 2
    @bakalolo: 10ms is a *very* long time. You can easily use OS-assisted sleep/wake stuff like a condition variable or just SIGSTOP / SIGCONT. If you're worried, make sure your kernel is using 1000HZ scheduling interval, not 100, so worst-case scheduling latency is something like 1ms plus a few microseconds to actually wake up a process. – Peter Cordes Oct 21 '19 at 23:33