0

Let there be a one-threaded programm, which calls a large functionLarge(), which needs to finish before the next codeline in the callers main Function. Assume the function is well broken down and just takes long.
In this answer .wait() is suggested and I wonder if its better than:

  bool done = false;
  // returning true at the end, modifies bigObject by refrence
  done =   functionLarge(bigObject);
  while(!done) { usleep(1000); }

  //...can now continue

Are there better approaches, without the returning bool?

Community
  • 1
  • 1
droid192
  • 2,011
  • 26
  • 43
  • Just stop using `bool` and let the program be ran in normal execution order? – MikeCAT Jul 23 '16 at 15:12
  • Your approach is bad because the value of `dont` won't magically change after assigned, so the loop will be taken either 0 or infinity times. – MikeCAT Jul 23 '16 at 15:14
  • you don't enter the while until `functionLarge` is done. – Dacav Jul 23 '16 at 15:15
  • Could you extend on "you dont enter before `functionLarge` is done". So at least it still blocks, also not as expected – droid192 Jul 23 '16 at 15:19
  • If this is truly a "one-threaded program" then functionLarge will be completely done when it returns, and there will be no reason to wait. – evan Jul 23 '16 at 15:30

1 Answers1

1

You're massively over-complicating this, because that's already how it works.

As you said, it's single-threaded. Control will be passed to functionLarge and will not be returned until that function has completed.

You do not need to do anything. No need for any bools or while loops. With a single thread, what is going to be doing the "waiting", exactly?

int main()
{
   doThis();
   nowDoThis();
}
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • The object is modified by `functionLarge` and in the next line added to an [interactive marker server](http://docs.ros.org/jade/api/interactive_markers/html/classinteractive__markers_1_1InteractiveMarkerServer.html#a7b688e99eeb8cdcc3f8d03c49ec5ef70). Without the construct I am not able to visualize the marker and I tracked it down, that the object is added before its done preparing by `functinLarge`. Also a simple `usleep(1000)` does work, so I assume its timing. I can directly visulize the difference in [Rviz](http://wiki.ros.org/rviz/Tutorials/Markers%3A%20Basic%20Shapes#Running_the_Code) – droid192 Jul 23 '16 at 15:25
  • ...oh I think I got it, the mistake was further up: the server is a dynamically allocated object and obviously not fully initalized, when I hand over the `bigObject`. – droid192 Jul 23 '16 at 16:02
  • No idea what you're talking about. – Lightness Races in Orbit Jul 23 '16 at 18:20