Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute and finish of methods

Tags:

c++

This is a very naive question, please forgive my ignorance if I use the wrong terms.

If I have a series of instructions as in the snippet,

bool methodComplete = false;
methodComplete = doSomeMethod(someParam, etcParam); //long & complex method that returns true
if (methodComplete)
   doSomeOtherMethod();

will the method doSomeMethod() finish its execution before if (methodComplete) is evaluated?

Or is this a case for an asynchronous pattern if I want to guarantee it is completed?

like image 680
garrilla Avatar asked Apr 06 '19 12:04

garrilla


2 Answers

The language specifications define how a program will effectively behave from the point of the user/programmer. So, yes, you can assume that the program behaves as that:

  • It computes doSomeMethod
  • It stores the results in methodComplete
  • It executes the if clauses

That said, some optimizations might result in code executed ahead, see Speculative execution.

like image 200
francesco Avatar answered Nov 08 '22 00:11

francesco


will the method doSomeMethod() finished executing before if (methodComplete) is evaluated?

Yes*.

or is this a case for an asynchronous pattern if I want to guarantee it has completed?

Only if you are doing parallel computing.


*)It can get to be a no if your code is executing in parallel..

like image 44
gsamaras Avatar answered Nov 08 '22 00:11

gsamaras