Hi, all:
I met a problem while executing the following code.
I need to check the vector to see if it is empty, if yes, remove the first element and process it. else go to the loop again.
While(true){
if(myVec.isEmpty()==false){
myObject=(myObject)myVec.remove(0);
//next process the object here
...
}
//else do nothing
}
But the problem is that in the while loop, it hangs there and never goes to the next loop.
After I added a time period of delay() in the loop, after if, everything is working now.
void delay(){
for(int i=0;i<1000;i++)
for(int j=0;j<1000;j++)
;
}
My question is that,
1) why does this cause a problem?
2) In multithreading, I can add sleep() in the thread run(), but this is a main thread, so there is no sleep here, my delay function is CPU entensive, very bad in fact, is there any better way to solve this?
Thanks so much!!!
I met a problem while executing the following code.
I need to check the vector to see if it is empty, if yes, remove the first element and process it. else go to the loop again.
While(true){
if(myVec.isEmpty()==false){
myObject=(myObject)myVec.remove(0);
//next process the object here
...
}
//else do nothing
}
But the problem is that in the while loop, it hangs there and never goes to the next loop.
After I added a time period of delay() in the loop, after if, everything is working now.
void delay(){
for(int i=0;i<1000;i++)
for(int j=0;j<1000;j++)
;
}
My question is that,
1) why does this cause a problem?
2) In multithreading, I can add sleep() in the thread run(), but this is a main thread, so there is no sleep here, my delay function is CPU entensive, very bad in fact, is there any better way to solve this?
Thanks so much!!!