droid192
droid192

Reputation: 2232

Wait until function finishes before continuing the main (one threaded)

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?

Upvotes: 0

Views: 4338

Answers (1)

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385144

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();
}

Upvotes: 1

Related Questions