假设有一个单线程程序,它调用一个大的functionLarge()
,它需要在调用者的下一个代码行之前完成 main
函数。假设功能被很好地分解并且只需要很长时间。
在这个answer建议使用 .wait()
,我想知道它是否比:
bool done = false;
// returning true at the end, modifies bigObject by refrence
done = functionLarge(bigObject);
while(!done) { usleep(1000); }
//...can now continue
有没有更好的方法,没有返回 bool
?
最佳答案
你把这个复杂化了很多,因为它已经是这样了。
如您所说,它是单线程。控制权将传递给 functionLarge
,并且在该函数完成之前不会返回。
您不需要做任何事情。不需要任何 bool
或 while
循环。对于单线程,“等待”究竟是什么?
int main()
{
doThis();
nowDoThis();
}
关于c++ - 等到函数完成后再继续 main(一个线程),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38543240/